Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
459670f363
|
|||
|
7636d0b5a6
|
|||
|
928ebb61b4
|
|||
|
7e85cdd8b0
|
|||
|
90074e6e32
|
|||
|
b8182e7538
|
|||
|
4331b9306e
|
|||
|
a08bc809bb
|
|||
|
1063369d96
|
|||
|
3a57d25a76
|
|||
|
8152edba39
|
|||
|
679db5b1db
|
|||
|
b1ad68c7b5
|
|||
|
280d6ec5c9
|
|||
|
68427c4a11
|
|||
|
8dd9e85b77
|
|||
|
0c7d2ad518
|
|||
|
0289dd4684
|
|||
|
e2d43315e3
|
|||
|
0e1c2c6350
|
|||
|
da85fc94b1
|
|||
|
c66d782eab
|
|||
|
358b44ab81
|
|||
|
5957d67bc3
|
|||
|
04410ea9e7
|
|||
|
a514cdc69f
|
|||
|
e822ba1f54
|
|||
|
3c925d8268
|
|||
|
c9073ff4a7
|
|||
|
88324c21c2
|
|||
|
ffc9741d1e
|
|||
| 5638315031 | |||
| 2e43a5e396 | |||
| baaefa2909 | |||
| cf268ebe0f | |||
| e571293f40 | |||
| fa92d90ff4 | |||
| 33aaa020c4 | |||
| bfc1812ebf | |||
| dba6d94eab | |||
| 09d2b6db8c | |||
| 0cbd975748 | |||
| c3cd01dbcd |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -21,3 +21,5 @@ test_output/
|
|||||||
|
|
||||||
# Config (for development)
|
# Config (for development)
|
||||||
config.toml
|
config.toml
|
||||||
|
.claude/
|
||||||
|
CLAUDE.md
|
||||||
108
CHANGELOG.md
108
CHANGELOG.md
@@ -7,33 +7,99 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Added
|
暂无。
|
||||||
|
|
||||||
- Initial release of QuiCommit
|
## [0.3.1] - 2026-06-01
|
||||||
- AI-powered commit message generation using LLM APIs (OpenAI, Anthropic) or local Ollama
|
|
||||||
- Support for Conventional Commits and @commitlint formats
|
### ✨ 新功能
|
||||||
- Multiple Git profile management with SSH and GPG support
|
- 按文件重要性对暂存差异排序,优先处理核心变更
|
||||||
- Smart tag generation with semantic version bumping
|
- DeepSeek 新增 reasoning 推理模式支持
|
||||||
- Automatic changelog generation
|
- LLM 统一思考模式配置,支持显式启用/禁用思考状态
|
||||||
- Interactive CLI with beautiful prompts and previews
|
- 新增 `thinking.rs` 思考状态管理模块
|
||||||
- Encrypted storage for sensitive data
|
|
||||||
- Cross-platform support (Linux, macOS, Windows)
|
### 🐞 错误修复
|
||||||
|
- 修复 Kimi 返回信息的读取错误
|
||||||
|
- 修复 DeepSeek 和 Kimi 流式响应的解析问题
|
||||||
|
|
||||||
|
### 📚 文档
|
||||||
|
- 新增 ROADMAP.md 项目路线图文档
|
||||||
|
|
||||||
|
### 🔧 其他变更
|
||||||
|
- LLM 模块大规模重构,所有提供商(Anthropic、DeepSeek、Kimi、Ollama、OpenAI、OpenRouter)适配流式响应处理
|
||||||
|
- 代码格式化并优化导入顺序
|
||||||
|
- 清理大量未使用的变量、方法及结构体警告
|
||||||
|
- 清理构建输出日志文件
|
||||||
|
- 重新编号 LLM 系统提示规则
|
||||||
|
- i18n 多语言消息格式修复
|
||||||
|
- 各命令模块(commit、tag、changelog、config、profile、init)持续优化
|
||||||
|
|
||||||
|
## [0.1.11] - 2026-03-23
|
||||||
|
|
||||||
|
### ✨ 新功能
|
||||||
|
- 新增配置导出导入功能,支持加密保护
|
||||||
|
- Profile 支持 Token 管理(PAT 等)
|
||||||
|
- 自动生成和维护 Keep a Changelog 格式的变更日志
|
||||||
|
- 交互式命令行界面,支持预览和确认
|
||||||
|
|
||||||
|
### 🔐 安全特性
|
||||||
|
- 敏感数据加密存储(API 密钥等)
|
||||||
|
- 使用系统密钥环安全保存凭证
|
||||||
|
|
||||||
|
### 🔧 其他变更
|
||||||
|
- 优化 diff 截断逻辑,使用字符边界确保多字节字符安全
|
||||||
|
- 改进配置管理器,支持修改追踪
|
||||||
|
|
||||||
|
## [0.1.9] - 2026-03-06
|
||||||
|
|
||||||
|
### 🐞 错误修复
|
||||||
|
- 修复diff截断时的字符边界问题
|
||||||
|
|
||||||
|
## [0.1.7] - 2026-02-14
|
||||||
|
|
||||||
|
### 🐞 错误修复
|
||||||
|
- 修复 `changelog` 命令默认覆盖文件的问题,现改为智能追加新版本条目到头部之后
|
||||||
|
|
||||||
|
### 🔧 其他变更
|
||||||
|
- 清理 `formatter.rs` 中未使用的函数(`format_commit_date`、`format_changelog_date`、`format_tag_name`、`truncate`、`format_markdown_list`、`format_changelog_section`、`format_git_config_key`)
|
||||||
|
- 清理 `validators.rs` 中未使用的函数(`validate_ssh_key`)
|
||||||
|
- 移除 `changelog` 命令的 `--prepend` 参数(默认行为已改为追加)
|
||||||
|
|
||||||
|
## [0.1.4] - 2026-02-01
|
||||||
|
|
||||||
|
### ✨ 新功能
|
||||||
|
- 新增 `test3.txt`,支持中文输出测试
|
||||||
|
- `generator` 模块新增 `language` 参数,可指定提交信息语言
|
||||||
|
- `commit` 与 `tag` 命令新增自动 push 功能
|
||||||
|
- 提交、标签及变更日志命令现支持多语言输出
|
||||||
|
- 新增 Kimi、DeepSeek、OpenRouter 三家 LLM 提供商支持
|
||||||
|
- 首次创建仓库,完成 0.1.0 版本基础功能
|
||||||
|
|
||||||
|
### 🐞 错误修复
|
||||||
|
- 修复 `git/commit.rs` 中的提交错误信息问题
|
||||||
|
- 修复 Git2 错误处理逻辑(仓库打开功能暂不可用)
|
||||||
|
- 统一代码风格(`rustfmt` 修正)
|
||||||
|
|
||||||
|
### 📚 文档
|
||||||
|
- 更新 README,补充新的安装方式与 CLI 选项说明
|
||||||
|
- 优化 README 内容,新增 LLM 提供商介绍
|
||||||
|
|
||||||
|
### 🔧 其他变更
|
||||||
|
- 新增个人访问令牌、使用统计与配置校验功能
|
||||||
|
- 添加 `test2.txt` 占位文件
|
||||||
|
|
||||||
|
## [0.1.0] - 2026-01-30
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Initial project structure
|
||||||
|
- Core functionality for git operations
|
||||||
|
- LLM integration
|
||||||
|
- Configuration management
|
||||||
|
- CLI interface
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- **Commit Generation**: Automatically generate conventional commit messages from git diffs
|
- **Commit Generation**: Automatically generate conventional commit messages from git diffs
|
||||||
- **Profile Management**: Switch between multiple Git identities for different contexts
|
- **Profile Management**: Switch between multiple Git identities for different contexts
|
||||||
- **Tag Management**: Create annotated tags with AI-generated release notes
|
- **Tag Management**: Create annotated tags with AI-generated release notes
|
||||||
- **Changelog**: Generate and maintain changelog in Keep a Changelog format
|
- **Changelog**: Generate and maintain changelog in Keep a Changelog format
|
||||||
- **Security**: Encrypt SSH passphrases and API keys
|
- **Security**: Encrypt SSH passphrases and API keys
|
||||||
- **Interactive UI**: Beautiful CLI with prompts and previews
|
- **Interactive UI**: Beautiful CLI with prompts and previews
|
||||||
|
|
||||||
## [0.1.0] - 2026-01-30
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
- Initial project structure
|
|
||||||
- Core functionality for git operations
|
|
||||||
- LLM integration
|
|
||||||
- Configuration management
|
|
||||||
- CLI interface
|
|
||||||
|
|||||||
20
Cargo.toml
20
Cargo.toml
@@ -1,11 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "quicommit"
|
name = "quicommit"
|
||||||
version = "0.1.0"
|
version = "0.3.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = ["Sidney Zhang <zly@lyzhang.me>"]
|
authors = ["Sidney Zhang <zly@lyzhang.me>"]
|
||||||
description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation"
|
description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/yourusername/quicommit"
|
repository = "https://git.lyz.one/SidneyZhang/QuiCommit"
|
||||||
keywords = ["git", "commit", "ai", "cli", "automation"]
|
keywords = ["git", "commit", "ai", "cli", "automation"]
|
||||||
categories = ["command-line-utilities", "development-tools"]
|
categories = ["command-line-utilities", "development-tools"]
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ git2 = "0.20.3"
|
|||||||
which = "6.0"
|
which = "6.0"
|
||||||
|
|
||||||
# HTTP client for LLM APIs
|
# HTTP client for LLM APIs
|
||||||
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false }
|
reqwest = { version = "0.12", features = ["json", "rustls-tls", "stream"], default-features = false }
|
||||||
tokio = { version = "1.35", features = ["full"] }
|
tokio = { version = "1.35", features = ["full"] }
|
||||||
|
|
||||||
# Error handling
|
# Error handling
|
||||||
@@ -57,6 +57,7 @@ sha2 = "0.10"
|
|||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
textwrap = "0.16"
|
textwrap = "0.16"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
futures-util = "0.3"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
atty = "0.2"
|
atty = "0.2"
|
||||||
|
|
||||||
@@ -66,6 +67,9 @@ argon2 = "0.5"
|
|||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
base64 = "0.22"
|
base64 = "0.22"
|
||||||
|
|
||||||
|
# System keyring for secure API key storage
|
||||||
|
keyring = { version = "3.6.3", features = ["apple-native", "windows-native", "sync-secret-service"] }
|
||||||
|
|
||||||
# Interactive editor
|
# Interactive editor
|
||||||
edit = "0.1"
|
edit = "0.1"
|
||||||
|
|
||||||
@@ -80,11 +84,13 @@ mockall = "0.12"
|
|||||||
wiremock = "0.6"
|
wiremock = "0.6"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = "s"
|
||||||
lto = true
|
lto = "thin"
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
panic = "abort"
|
||||||
strip = true
|
strip = true
|
||||||
|
debug = false
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 0
|
opt-level = 1
|
||||||
debug = true
|
debug = true
|
||||||
|
|||||||
128
RAODMAP.md
Normal file
128
RAODMAP.md
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
# QuiCommit Roadmap
|
||||||
|
|
||||||
|
## 已完成 ✅
|
||||||
|
|
||||||
|
- [x] 基础 Git 操作(commit、tag、changelog)
|
||||||
|
- [x] AI 驱动提交信息生成(Conventional Commits / commitlint 格式)
|
||||||
|
- [x] 多 LLM 提供商支持:Ollama、OpenAI、Anthropic、Kimi、DeepSeek、OpenRouter
|
||||||
|
- [x] 多 Git Profile 管理(SSH 密钥 + GPG 签名)
|
||||||
|
- [x] 语义化版本自动升级与 AI 发布说明
|
||||||
|
- [x] Keep a Changelog 格式自动生成
|
||||||
|
- [x] 系统密钥环安全存储 API Key
|
||||||
|
- [x] 敏感数据加密存储(AES-GCM + Argon2)
|
||||||
|
- [x] 交互式 CLI 预览与确认
|
||||||
|
- [x] 7 种语言国际化支持
|
||||||
|
- [x] 配置导出/导入(支持加密保护)
|
||||||
|
- [x] Profile Token 管理(PAT 等)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 进行中 🚧
|
||||||
|
|
||||||
|
暂无。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 计划中 📋
|
||||||
|
|
||||||
|
### 1. Git 凭证管理器
|
||||||
|
|
||||||
|
将 Git 凭证管理集成到 QuiCommit 中,统一管理 HTTPS 仓库的身份认证。
|
||||||
|
|
||||||
|
- [ ] **Git Credential Helper 集成**
|
||||||
|
- 实现 `git credential-store` / `git-credential-libsecret` 等标准的 credential helper 协议
|
||||||
|
- 支持 `quicommit credential get|store|erase` 子命令
|
||||||
|
- 与系统密钥环无缝对接,复用已有的 `KeyringManager`
|
||||||
|
|
||||||
|
- [ ] **凭证管理 CLI**
|
||||||
|
- `quicommit credential list` — 列出所有已存储的凭证
|
||||||
|
- `quicommit credential add` — 手动添加凭证(用户名 + 密码/Token)
|
||||||
|
- `quicommit credential remove` — 删除指定凭证
|
||||||
|
- `quicommit credential status` — 查看凭证管理状态
|
||||||
|
|
||||||
|
- [ ] **跨平台支持**
|
||||||
|
- Windows:集成 Windows Credential Manager
|
||||||
|
- macOS:集成 Keychain
|
||||||
|
- Linux:通过 Secret Service / D-Bus 对接 GNOME Keyring / KWallet
|
||||||
|
|
||||||
|
- [ ] **安全增强**
|
||||||
|
- 支持 PAT(Personal Access Token)按 scope / 有效期管理
|
||||||
|
- 支持凭证过期检查和自动提醒
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. 新增模型支持
|
||||||
|
|
||||||
|
扩展 LLM 提供商和模型覆盖范围,满足更多场景和偏好。
|
||||||
|
|
||||||
|
- [x] **新增 DeepSeek 最新模型**
|
||||||
|
- 支持 `deepseek-chat`(DeepSeek-V3)
|
||||||
|
- 支持 `deepseek-reasoner`(DeepSeek-R1)
|
||||||
|
- 支持 `deepseek-v4`
|
||||||
|
|
||||||
|
- [ ] **新增国内模型提供商**
|
||||||
|
- 通义千问 (Qwen) — 阿里云 DashScope API
|
||||||
|
- 文心一言 (ERNIE) — 百度千帆 API
|
||||||
|
- 智谱 GLM — ChatGLM API
|
||||||
|
- 百川 (Baichuan) — Baichuan API
|
||||||
|
|
||||||
|
- [ ] **新增国际模型提供商**
|
||||||
|
- Google Gemini API
|
||||||
|
- Mistral AI API
|
||||||
|
- Cohere API
|
||||||
|
- Groq (LPU 推理加速)
|
||||||
|
|
||||||
|
- [ ] **本地模型扩展**
|
||||||
|
- 支持 llama.cpp 服务端(兼容 OpenAI API 格式)
|
||||||
|
- 支持 vLLM 部署的模型
|
||||||
|
- 本地模型推荐列表与一键配置向导
|
||||||
|
|
||||||
|
- [ ] **模型能力适配**
|
||||||
|
- 不同模型的 token 限制自适应
|
||||||
|
- 模型特定的 prompt 模板优化
|
||||||
|
- 支持 function calling / tool use(用于复杂生成场景)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. 生成体验优化
|
||||||
|
|
||||||
|
提升 AI 生成提交信息、标签说明和变更日志时的用户体验。
|
||||||
|
|
||||||
|
- [x] **流式输出与实时反馈**
|
||||||
|
- 支持 SSE(Server-Sent Events)流式生成
|
||||||
|
- 终端打字机效果实时显示生成内容
|
||||||
|
- 流式生成过程中支持 `Ctrl+C` 中断
|
||||||
|
|
||||||
|
- [ ] **生成质量提升**
|
||||||
|
- 基于 commitlint 规则的后校验与自动修正
|
||||||
|
- 支持 Few-shot 示例引导(用户可自定义示例库)
|
||||||
|
- 生成结果的置信度评分与多候选方案
|
||||||
|
|
||||||
|
- [ ] **Diff 上下文增强**
|
||||||
|
- 智能 diff 摘要(大改动时自动压缩关键信息)
|
||||||
|
- 支持 `.gitattributes` 排除/包含规则
|
||||||
|
- 按文件类型分组生成更精准的提交描述
|
||||||
|
|
||||||
|
- [ ] **交互式编辑增强**
|
||||||
|
- 生成后支持内联编辑(类似 `git rebase -i` 体验)
|
||||||
|
- 支持重新生成指定部分(如 scope、description)
|
||||||
|
- 历史提交信息学习与风格适配
|
||||||
|
|
||||||
|
- [ ] **批量操作支持**
|
||||||
|
- 批量生成多个 commit(分组暂存区变更)
|
||||||
|
- `--dry-run` 预览模式(只生成本地查看,不写 Git)
|
||||||
|
|
||||||
|
- [ ] **性能优化**
|
||||||
|
- API 请求并发优化(多个模型同时生成候选)
|
||||||
|
- 本地缓存常用 prompt 模板
|
||||||
|
- 减少不必要的 diff 计算
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 长远规划 🌟
|
||||||
|
|
||||||
|
- [ ] **VS Code 扩展** — 在 IDE 内直接使用 QuiCommit
|
||||||
|
- [ ] **GitHub Action / GitLab CI 集成** — 自动化 PR 标题和描述生成
|
||||||
|
- [ ] **团队协作** — 共享 commit 风格配置、prompt 模板库
|
||||||
|
- [ ] **Web Dashboard** — 可视化管理多仓库的 Git 活动与 AI 生成统计
|
||||||
|
- [ ] **插件系统** — 允许社区贡献自定义 LLM 提供商和生成策略
|
||||||
282
README.md
282
README.md
@@ -4,8 +4,14 @@ English | [中文文档](README_zh.md)
|
|||||||
|
|
||||||
A powerful AI-powered Git assistant for generating conventional commits, tags, and changelogs. Manage multiple Git profiles for different work contexts.
|
A powerful AI-powered Git assistant for generating conventional commits, tags, and changelogs. Manage multiple Git profiles for different work contexts.
|
||||||
|
|
||||||
|
[Still in early development, some features may not be complete. Feedback and contributions are welcome.]
|
||||||
|
|
||||||
|
> ⚠️ **Important Notice**: QuiCommit now uses system keyring to store API keys securely. This change may cause breaking changes to your existing configuration. If you encounter issues after updating, please run `quicommit config reset --force` to reset your configuration, then reconfigure your settings.
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -14,14 +20,26 @@ A powerful AI-powered Git assistant for generating conventional commits, tags, a
|
|||||||
- **Profile Management**: Manage multiple Git identities with SSH keys and GPG signing support
|
- **Profile Management**: Manage multiple Git identities with SSH keys and GPG signing support
|
||||||
- **Smart Tagging**: Semantic version bumping with AI-generated release notes
|
- **Smart Tagging**: Semantic version bumping with AI-generated release notes
|
||||||
- **Changelog Generation**: Automatic changelog generation in Keep a Changelog format
|
- **Changelog Generation**: Automatic changelog generation in Keep a Changelog format
|
||||||
- **Security**: Encrypt sensitive data
|
- **Security**: Use system keyring to store API keys securely
|
||||||
- **Interactive UI**: Beautiful CLI with previews and confirmations
|
- **Interactive UI**: Beautiful CLI with previews and confirmations
|
||||||
|
- **Multi-language Support**: Output in 7 languages (English, Chinese, Japanese, Korean, Spanish, French, German)
|
||||||
|
- **Config Export/Import**: Backup and restore configuration with optional encryption
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
### Cargo Install
|
||||||
|
|
||||||
|
The cargo-installed version may temporarily lag behind the source code progress.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/yourusername/quicommit.git
|
cargo install quicommit
|
||||||
cd quicommit
|
```
|
||||||
|
|
||||||
|
### Install from Source
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.lyz.one/SidneyZhang/QuiCommit.git
|
||||||
|
cd QuiCommit
|
||||||
cargo build --release
|
cargo build --release
|
||||||
cargo install --path .
|
cargo install --path .
|
||||||
```
|
```
|
||||||
@@ -50,6 +68,12 @@ quicommit commit -a
|
|||||||
|
|
||||||
# Skip confirmation
|
# Skip confirmation
|
||||||
quicommit commit --yes
|
quicommit commit --yes
|
||||||
|
|
||||||
|
# Use date-based commit message
|
||||||
|
quicommit commit --date
|
||||||
|
|
||||||
|
# Push after committing
|
||||||
|
quicommit commit --push
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create Tag
|
### Create Tag
|
||||||
@@ -63,6 +87,12 @@ quicommit tag --bump minor
|
|||||||
|
|
||||||
# Custom tag name
|
# Custom tag name
|
||||||
quicommit tag -n v1.0.0
|
quicommit tag -n v1.0.0
|
||||||
|
|
||||||
|
# AI-generate tag message
|
||||||
|
quicommit tag --generate
|
||||||
|
|
||||||
|
# Create tag and push to remote
|
||||||
|
quicommit tag --push
|
||||||
```
|
```
|
||||||
|
|
||||||
### Generate Changelog
|
### Generate Changelog
|
||||||
@@ -73,6 +103,15 @@ quicommit changelog
|
|||||||
|
|
||||||
# Generate for specific version
|
# Generate for specific version
|
||||||
quicommit changelog -v 1.0.0
|
quicommit changelog -v 1.0.0
|
||||||
|
|
||||||
|
# AI-generate changelog
|
||||||
|
quicommit changelog --generate
|
||||||
|
|
||||||
|
# Initialize new changelog file
|
||||||
|
quicommit changelog --init
|
||||||
|
|
||||||
|
# Specify output file
|
||||||
|
quicommit changelog -o RELEASE_NOTES.md
|
||||||
```
|
```
|
||||||
|
|
||||||
### Manage Profiles
|
### Manage Profiles
|
||||||
@@ -84,11 +123,41 @@ quicommit profile add
|
|||||||
# List profiles
|
# List profiles
|
||||||
quicommit profile list
|
quicommit profile list
|
||||||
|
|
||||||
|
# Show profile details
|
||||||
|
quicommit profile show
|
||||||
|
|
||||||
# Switch profile
|
# Switch profile
|
||||||
quicommit profile switch
|
quicommit profile switch
|
||||||
|
|
||||||
|
# Set default profile
|
||||||
|
quicommit profile set-default personal
|
||||||
|
|
||||||
# Set profile for current repo
|
# Set profile for current repo
|
||||||
quicommit profile set-repo personal
|
quicommit profile set-repo personal
|
||||||
|
|
||||||
|
# Apply profile to current repo
|
||||||
|
quicommit profile apply
|
||||||
|
|
||||||
|
# Apply profile globally
|
||||||
|
quicommit profile apply --global
|
||||||
|
|
||||||
|
# Copy profile
|
||||||
|
quicommit profile copy personal work
|
||||||
|
|
||||||
|
# Edit profile
|
||||||
|
quicommit profile edit personal
|
||||||
|
|
||||||
|
# Remove profile
|
||||||
|
quicommit profile remove old-profile
|
||||||
|
|
||||||
|
# Check profile
|
||||||
|
quicommit profile check
|
||||||
|
|
||||||
|
# View usage statistics
|
||||||
|
quicommit profile stats
|
||||||
|
|
||||||
|
# Manage profile tokens
|
||||||
|
quicommit profile token
|
||||||
```
|
```
|
||||||
|
|
||||||
### Configure LLM
|
### Configure LLM
|
||||||
@@ -96,30 +165,68 @@ quicommit profile set-repo personal
|
|||||||
```bash
|
```bash
|
||||||
# Configure Ollama (local)
|
# Configure Ollama (local)
|
||||||
quicommit config set-llm ollama
|
quicommit config set-llm ollama
|
||||||
quicommit config set-ollama --url http://localhost:11434 --model llama2
|
quicommit config set-llm ollama --url http://localhost:11434 --model llama2
|
||||||
|
|
||||||
# Configure OpenAI
|
# Configure OpenAI
|
||||||
quicommit config set-llm openai
|
quicommit config set-llm openai
|
||||||
quicommit config set-openai-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
# Configure Anthropic Claude
|
# Configure Anthropic Claude
|
||||||
quicommit config set-llm anthropic
|
quicommit config set-llm anthropic
|
||||||
quicommit config set-anthropic-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
# Configure Kimi (Moonshot AI)
|
# Configure Kimi (Moonshot AI)
|
||||||
quicommit config set-llm kimi
|
quicommit config set-llm kimi
|
||||||
quicommit config set-kimi-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||||
|
|
||||||
# Configure DeepSeek
|
# Configure DeepSeek
|
||||||
quicommit config set-llm deepseek
|
quicommit config set-llm deepseek
|
||||||
quicommit config set-deepseek-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||||
|
|
||||||
# Configure OpenRouter
|
# Configure OpenRouter
|
||||||
quicommit config set-llm openrouter
|
quicommit config set-llm openrouter
|
||||||
quicommit config set-openrouter-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||||
|
|
||||||
|
# Set commit format
|
||||||
|
quicommit config set-commit-format conventional
|
||||||
|
|
||||||
|
# Set version prefix
|
||||||
|
quicommit config set-version-prefix v
|
||||||
|
|
||||||
|
# Set changelog path
|
||||||
|
quicommit config set-changelog-path CHANGELOG.md
|
||||||
|
|
||||||
|
# Set output language (en, zh, ja, ko, es, fr, de)
|
||||||
|
quicommit config set-language en
|
||||||
|
|
||||||
|
# Set keep commit types in English
|
||||||
|
quicommit config set-keep-types-english true
|
||||||
|
|
||||||
|
# Set keep changelog types in English
|
||||||
|
quicommit config set-keep-changelog-types-english true
|
||||||
|
|
||||||
# Test LLM connection
|
# Test LLM connection
|
||||||
quicommit config test-llm
|
quicommit config test-llm
|
||||||
|
|
||||||
|
# Check keyring availability
|
||||||
|
quicommit config check-keyring
|
||||||
|
|
||||||
|
# Show config file path
|
||||||
|
quicommit config path
|
||||||
|
|
||||||
|
# Export configuration (with optional encryption)
|
||||||
|
quicommit config export -o config-backup.toml
|
||||||
|
quicommit config export -o config-backup.enc --password
|
||||||
|
|
||||||
|
# Import configuration
|
||||||
|
quicommit config import -i config-backup.toml
|
||||||
|
quicommit config import -i config-backup.enc --password
|
||||||
|
|
||||||
|
# Reset configuration to defaults
|
||||||
|
quicommit config reset --force
|
||||||
```
|
```
|
||||||
|
|
||||||
## Command Reference
|
## Command Reference
|
||||||
@@ -137,17 +244,23 @@ quicommit config test-llm
|
|||||||
|
|
||||||
| Option | Description |
|
| Option | Description |
|
||||||
|--------|-------------|
|
|--------|-------------|
|
||||||
| `-t, --commit-type` | Commit type (feat, fix, etc.) |
|
| `--commit-type` | Commit type (feat, fix, etc.) |
|
||||||
| `-s, --scope` | Commit scope |
|
| `-s, --scope` | Commit scope |
|
||||||
| `-m, --message` | Commit description |
|
| `-m, --message` | Commit description |
|
||||||
| `--body` | Commit body |
|
| `--body` | Commit body |
|
||||||
| `--breaking` | Mark as breaking change |
|
| `-b, --breaking` | Mark as breaking change |
|
||||||
|
| `-d, --date` | Use date-based commit message |
|
||||||
| `--manual` | Manual input, skip AI |
|
| `--manual` | Manual input, skip AI |
|
||||||
| `-a, --all` | Stage all changes |
|
| `-a, --all` | Stage all changes |
|
||||||
| `-S, --sign` | GPG sign commit |
|
| `-S, --sign` | GPG sign commit |
|
||||||
| `--amend` | Amend previous commit |
|
| `--amend` | Amend previous commit |
|
||||||
| `--dry-run` | Show without committing |
|
| `--dry-run` | Show without committing |
|
||||||
|
| `--conventional` | Use Conventional Commits format |
|
||||||
|
| `--commitlint` | Use commitlint format |
|
||||||
|
| `--no-verify` | Skip commit message verification |
|
||||||
| `-y, --yes` | Skip confirmation |
|
| `-y, --yes` | Skip confirmation |
|
||||||
|
| `--push` | Push after committing |
|
||||||
|
| `--remote` | Specify remote repository (default: origin) |
|
||||||
|
|
||||||
### Tag Options
|
### Tag Options
|
||||||
|
|
||||||
@@ -158,14 +271,35 @@ quicommit config test-llm
|
|||||||
| `-m, --message` | Tag message |
|
| `-m, --message` | Tag message |
|
||||||
| `-g, --generate` | AI-generate message |
|
| `-g, --generate` | AI-generate message |
|
||||||
| `-S, --sign` | GPG sign tag |
|
| `-S, --sign` | GPG sign tag |
|
||||||
| `--lightweight` | Create lightweight tag |
|
| `-l, --lightweight` | Create lightweight tag |
|
||||||
| `--push` | Push to remote |
|
| `-f, --force` | Force overwrite existing tag |
|
||||||
|
| `-p, --push` | Push to remote |
|
||||||
|
| `-r, --remote` | Specify remote repository (default: origin) |
|
||||||
|
| `--dry-run` | Dry run |
|
||||||
|
| `-y, --yes` | Skip confirmation |
|
||||||
|
|
||||||
|
### Changelog Options
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `-o, --output` | Output file path |
|
||||||
|
| `-v, --version` | Generate for specific version |
|
||||||
|
| `-f, --from` | Generate from specific tag |
|
||||||
|
| `-t, --to` | Generate to specific ref (default: HEAD) |
|
||||||
|
| `-i, --init` | Initialize new changelog file |
|
||||||
|
| `-g, --generate` | AI-generate changelog |
|
||||||
|
| `--prepend` | Prepend to existing changelog |
|
||||||
|
| `--include-hashes` | Include commit hashes |
|
||||||
|
| `--include-authors` | Include authors |
|
||||||
|
| `--format` | Format (keep-a-changelog, github-releases) |
|
||||||
|
| `--dry-run` | Dry run (output to stdout) |
|
||||||
| `-y, --yes` | Skip confirmation |
|
| `-y, --yes` | Skip confirmation |
|
||||||
|
|
||||||
## Configuration File
|
## Configuration File
|
||||||
|
|
||||||
Location:
|
Location:
|
||||||
- Linux/macOS: `~/.config/quicommit/config.toml`
|
- Linux: `~/.config/quicommit/config.toml`
|
||||||
|
- macOS: `~/Library/Application Support/quicommit/config.toml`
|
||||||
- Windows: `%APPDATA%\quicommit\config.toml`
|
- Windows: `%APPDATA%\quicommit\config.toml`
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
@@ -174,15 +308,27 @@ default_profile = "personal"
|
|||||||
|
|
||||||
[profiles.personal]
|
[profiles.personal]
|
||||||
name = "personal"
|
name = "personal"
|
||||||
user_name = "John Doe"
|
user_name = "Your Name"
|
||||||
user_email = "john@example.com"
|
user_email = "your.email@example.com"
|
||||||
|
description = "Personal projects"
|
||||||
|
is_work = false
|
||||||
|
|
||||||
[profiles.work]
|
[profiles.work]
|
||||||
name = "work"
|
name = "work"
|
||||||
user_name = "John Doe"
|
user_name = "Your Name"
|
||||||
user_email = "john@company.com"
|
user_email = "your.name@company.com"
|
||||||
|
description = "Work projects"
|
||||||
is_work = true
|
is_work = true
|
||||||
organization = "Acme Corp"
|
organization = "Your Company"
|
||||||
|
|
||||||
|
[profiles.work.ssh]
|
||||||
|
private_key_path = "/home/user/.ssh/id_rsa_work"
|
||||||
|
agent_forwarding = true
|
||||||
|
|
||||||
|
[profiles.work.gpg]
|
||||||
|
key_id = "YOUR_GPG_KEY_ID"
|
||||||
|
program = "gpg"
|
||||||
|
use_agent = true
|
||||||
|
|
||||||
[llm]
|
[llm]
|
||||||
provider = "ollama"
|
provider = "ollama"
|
||||||
@@ -198,19 +344,50 @@ model = "llama2"
|
|||||||
model = "gpt-4"
|
model = "gpt-4"
|
||||||
base_url = "https://api.openai.com/v1"
|
base_url = "https://api.openai.com/v1"
|
||||||
|
|
||||||
|
[llm.anthropic]
|
||||||
|
model = "claude-3-sonnet-20240229"
|
||||||
|
|
||||||
|
[llm.kimi]
|
||||||
|
model = "moonshot-v1-8k"
|
||||||
|
|
||||||
|
[llm.deepseek]
|
||||||
|
model = "deepseek-chat"
|
||||||
|
|
||||||
|
[llm.openrouter]
|
||||||
|
model = "openai/gpt-4"
|
||||||
|
|
||||||
[commit]
|
[commit]
|
||||||
format = "conventional"
|
format = "conventional"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
allow_empty = false
|
||||||
|
gpg_sign = false
|
||||||
max_subject_length = 100
|
max_subject_length = 100
|
||||||
|
require_scope = false
|
||||||
|
require_body = false
|
||||||
|
body_required_types = ["feat", "fix"]
|
||||||
|
|
||||||
[tag]
|
[tag]
|
||||||
version_prefix = "v"
|
version_prefix = "v"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
gpg_sign = false
|
||||||
|
include_changelog = true
|
||||||
|
|
||||||
[changelog]
|
[changelog]
|
||||||
path = "CHANGELOG.md"
|
path = "CHANGELOG.md"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
format = "keep-a-changelog"
|
||||||
|
include_hashes = false
|
||||||
|
include_authors = false
|
||||||
group_by_type = true
|
group_by_type = true
|
||||||
|
|
||||||
|
[theme]
|
||||||
|
colors = true
|
||||||
|
icons = true
|
||||||
|
date_format = "%Y-%m-%d"
|
||||||
|
|
||||||
|
[repo_profiles]
|
||||||
|
"/path/to/work/project" = "work"
|
||||||
|
"/path/to/personal/project" = "personal"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
@@ -227,14 +404,46 @@ group_by_type = true
|
|||||||
# View current configuration
|
# View current configuration
|
||||||
quicommit config list
|
quicommit config list
|
||||||
|
|
||||||
|
# Show configuration details
|
||||||
|
quicommit config show
|
||||||
|
|
||||||
|
# Edit configuration file
|
||||||
|
quicommit config edit
|
||||||
|
|
||||||
|
# Set configuration value
|
||||||
|
quicommit config set llm.provider ollama
|
||||||
|
|
||||||
|
# Get configuration value
|
||||||
|
quicommit config get llm.provider
|
||||||
|
|
||||||
|
# Set API key (stored in system keyring)
|
||||||
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
|
# Delete API key from keyring
|
||||||
|
quicommit config delete-api-key
|
||||||
|
|
||||||
# Test LLM connection
|
# Test LLM connection
|
||||||
quicommit config test-llm
|
quicommit config test-llm
|
||||||
|
|
||||||
# List available models
|
# List available models
|
||||||
quicommit config list-models
|
quicommit config list-models
|
||||||
|
|
||||||
# Edit configuration
|
# Check keyring availability
|
||||||
quicommit config edit
|
quicommit config check-keyring
|
||||||
|
|
||||||
|
# Show config file path
|
||||||
|
quicommit config path
|
||||||
|
|
||||||
|
# Export configuration (with optional encryption)
|
||||||
|
quicommit config export -o config-backup.toml
|
||||||
|
quicommit config export -o config-backup.enc --password
|
||||||
|
|
||||||
|
# Import configuration
|
||||||
|
quicommit config import -i config-backup.toml
|
||||||
|
quicommit config import -i config-backup.enc --password
|
||||||
|
|
||||||
|
# Reset configuration
|
||||||
|
quicommit config reset --force
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
@@ -253,8 +462,8 @@ Contributions are welcome! Please follow these steps:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Clone repository
|
# Clone repository
|
||||||
git clone https://github.com/YOUR_USERNAME/quicommit.git
|
git clone https://git.lyz.one/SidneyZhang/QuiCommit.git
|
||||||
cd quicommit
|
cd QuiCommit
|
||||||
|
|
||||||
# Fetch dependencies
|
# Fetch dependencies
|
||||||
cargo fetch
|
cargo fetch
|
||||||
@@ -282,11 +491,36 @@ cargo fmt --check
|
|||||||
```
|
```
|
||||||
src/
|
src/
|
||||||
├── commands/ # CLI command implementations
|
├── commands/ # CLI command implementations
|
||||||
|
│ ├── commit.rs
|
||||||
|
│ ├── tag.rs
|
||||||
|
│ ├── changelog.rs
|
||||||
|
│ ├── profile.rs
|
||||||
|
│ ├── config.rs
|
||||||
|
│ └── init.rs
|
||||||
├── config/ # Configuration management
|
├── config/ # Configuration management
|
||||||
|
│ ├── manager.rs
|
||||||
|
│ └── profile.rs
|
||||||
├── generator/ # AI content generation
|
├── generator/ # AI content generation
|
||||||
├── git/ # Git operations
|
├── git/ # Git operations
|
||||||
|
│ ├── commit.rs
|
||||||
|
│ ├── tag.rs
|
||||||
|
│ └── changelog.rs
|
||||||
├── llm/ # LLM provider implementations
|
├── llm/ # LLM provider implementations
|
||||||
└── utils/ # Utility functions
|
│ ├── ollama.rs
|
||||||
|
│ ├── openai.rs
|
||||||
|
│ ├── anthropic.rs
|
||||||
|
│ ├── kimi.rs
|
||||||
|
│ ├── deepseek.rs
|
||||||
|
│ └── openrouter.rs
|
||||||
|
├── i18n/ # Internationalization support
|
||||||
|
│ ├── messages.rs
|
||||||
|
│ └── translator.rs
|
||||||
|
├── utils/ # Utility functions
|
||||||
|
│ ├── validators.rs
|
||||||
|
│ ├── formatter.rs
|
||||||
|
│ ├── crypto.rs
|
||||||
|
│ └── editor.rs
|
||||||
|
└── main.rs # Program entry point
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
5
build.rs
5
build.rs
@@ -1,10 +1,11 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Only generate completions when explicitly requested
|
// Only generate completions when explicitly requested
|
||||||
if env::var("GENERATE_COMPLETIONS").is_ok() {
|
if env::var("GENERATE_COMPLETIONS").is_ok() {
|
||||||
println!("cargo:warning=To generate shell completions, run: cargo run --bin quicommit -- completions");
|
println!(
|
||||||
|
"cargo:warning=To generate shell completions, run: cargo run --bin quicommit -- completions"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rerun if build.rs changes
|
// Rerun if build.rs changes
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
# - macOS: ~/Library/Application Support/quicommit/config.toml
|
# - macOS: ~/Library/Application Support/quicommit/config.toml
|
||||||
# - Windows: %APPDATA%\quicommit\config.toml
|
# - Windows: %APPDATA%\quicommit\config.toml
|
||||||
|
|
||||||
|
# ⚠️ IMPORTANT: Keyring Feature Update
|
||||||
|
# QuiCommit now uses system keyring to store API keys securely.
|
||||||
|
# This change may cause breaking changes to your existing configuration.
|
||||||
|
# If you encounter issues after updating, please reset your configuration:
|
||||||
|
# quicommit config reset --force
|
||||||
|
# Then reconfigure your settings using the CLI commands.
|
||||||
|
|
||||||
# Configuration version (for migration)
|
# Configuration version (for migration)
|
||||||
version = "1"
|
version = "1"
|
||||||
|
|
||||||
|
|||||||
287
readme_zh.md
287
readme_zh.md
@@ -4,8 +4,13 @@
|
|||||||
|
|
||||||
一款强大的AI驱动的Git助手,用于生成规范化的提交信息、标签和变更日志,并支持管理多个Git配置。
|
一款强大的AI驱动的Git助手,用于生成规范化的提交信息、标签和变更日志,并支持管理多个Git配置。
|
||||||
|
|
||||||

|
【目前还处在早期开发阶段,依然有一些功能未完善,欢迎反馈和贡献。】
|
||||||

|
|
||||||
|
> ⚠️ **重要提示**:QuiCommit 现在使用系统密钥环(keyring)来安全存储 API 密钥。此更改可能会对现有配置造成破坏性变更。如果在更新后遇到问题,请运行 `quicommit config reset --force` 重置配置,然后重新配置您的设置。
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
## 主要功能
|
## 主要功能
|
||||||
|
|
||||||
@@ -14,16 +19,26 @@
|
|||||||
- **多配置管理**:为不同场景管理多个Git身份,支持SSH密钥和GPG签名配置
|
- **多配置管理**:为不同场景管理多个Git身份,支持SSH密钥和GPG签名配置
|
||||||
- **智能标签管理**:基于语义版本自动检测升级,AI生成标签信息
|
- **智能标签管理**:基于语义版本自动检测升级,AI生成标签信息
|
||||||
- **变更日志生成**:自动生成Keep a Changelog格式的变更日志
|
- **变更日志生成**:自动生成Keep a Changelog格式的变更日志
|
||||||
- **安全保护**:加密存储敏感数据
|
- **安全保护**:使用系统密钥环进行安全存储
|
||||||
- **交互式界面**:美观的CLI界面,支持预览和确认
|
- **交互式界面**:美观的CLI界面,支持预览和确认
|
||||||
|
- **多语言支持**:支持7种语言输出(中文、英语、日语、韩语、西班牙语、法语、德语)
|
||||||
|
- **配置导出导入**:备份和恢复配置,支持加密保护
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
目前,整体工具还在开发,并不保证各项功能准确达到既定目标。但依然十分欢迎参与贡献、反馈问题和建议。
|
### cargo安装
|
||||||
|
|
||||||
|
cargo安装版本可能暂时不如源码进展快速。
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/yourusername/quicommit.git
|
cargo install quicommit
|
||||||
cd quicommit
|
```
|
||||||
|
|
||||||
|
### 从源代码安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.lyz.one/SidneyZhang/QuiCommit.git
|
||||||
|
cd QuiCommit
|
||||||
cargo build --release
|
cargo build --release
|
||||||
cargo install --path .
|
cargo install --path .
|
||||||
```
|
```
|
||||||
@@ -52,6 +67,12 @@ quicommit commit -a
|
|||||||
|
|
||||||
# 跳过确认直接提交
|
# 跳过确认直接提交
|
||||||
quicommit commit --yes
|
quicommit commit --yes
|
||||||
|
|
||||||
|
# 使用日期格式的提交信息
|
||||||
|
quicommit commit --date
|
||||||
|
|
||||||
|
# 提交后推送到远程
|
||||||
|
quicommit commit --push
|
||||||
```
|
```
|
||||||
|
|
||||||
### 创建标签
|
### 创建标签
|
||||||
@@ -65,6 +86,12 @@ quicommit tag --bump minor
|
|||||||
|
|
||||||
# 自定义标签名
|
# 自定义标签名
|
||||||
quicommit tag -n v1.0.0
|
quicommit tag -n v1.0.0
|
||||||
|
|
||||||
|
# AI生成标签信息
|
||||||
|
quicommit tag --generate
|
||||||
|
|
||||||
|
# 创建标签并推送到远程
|
||||||
|
quicommit tag --push
|
||||||
```
|
```
|
||||||
|
|
||||||
### 生成变更日志
|
### 生成变更日志
|
||||||
@@ -75,6 +102,15 @@ quicommit changelog
|
|||||||
|
|
||||||
# 为特定版本生成
|
# 为特定版本生成
|
||||||
quicommit changelog -v 1.0.0
|
quicommit changelog -v 1.0.0
|
||||||
|
|
||||||
|
# AI生成变更日志
|
||||||
|
quicommit changelog --generate
|
||||||
|
|
||||||
|
# 初始化新的变更日志文件
|
||||||
|
quicommit changelog --init
|
||||||
|
|
||||||
|
# 指定输出文件
|
||||||
|
quicommit changelog -o RELEASE_NOTES.md
|
||||||
```
|
```
|
||||||
|
|
||||||
### 配置管理
|
### 配置管理
|
||||||
@@ -86,11 +122,41 @@ quicommit profile add
|
|||||||
# 查看配置列表
|
# 查看配置列表
|
||||||
quicommit profile list
|
quicommit profile list
|
||||||
|
|
||||||
|
# 显示配置详情
|
||||||
|
quicommit profile show
|
||||||
|
|
||||||
# 切换配置
|
# 切换配置
|
||||||
quicommit profile switch
|
quicommit profile switch
|
||||||
|
|
||||||
|
# 设置默认配置
|
||||||
|
quicommit profile set-default personal
|
||||||
|
|
||||||
# 设置当前仓库的配置
|
# 设置当前仓库的配置
|
||||||
quicommit profile set-repo personal
|
quicommit profile set-repo personal
|
||||||
|
|
||||||
|
# 应用配置到当前仓库
|
||||||
|
quicommit profile apply
|
||||||
|
|
||||||
|
# 全局应用配置
|
||||||
|
quicommit profile apply --global
|
||||||
|
|
||||||
|
# 复制配置
|
||||||
|
quicommit profile copy personal work
|
||||||
|
|
||||||
|
# 编辑配置
|
||||||
|
quicommit profile edit personal
|
||||||
|
|
||||||
|
# 删除配置
|
||||||
|
quicommit profile remove old-profile
|
||||||
|
|
||||||
|
# 检查配置
|
||||||
|
quicommit profile check
|
||||||
|
|
||||||
|
# 查看使用统计
|
||||||
|
quicommit profile stats
|
||||||
|
|
||||||
|
# 管理配置的令牌
|
||||||
|
quicommit profile token
|
||||||
```
|
```
|
||||||
|
|
||||||
### LLM配置
|
### LLM配置
|
||||||
@@ -98,30 +164,68 @@ quicommit profile set-repo personal
|
|||||||
```bash
|
```bash
|
||||||
# 配置Ollama(本地)
|
# 配置Ollama(本地)
|
||||||
quicommit config set-llm ollama
|
quicommit config set-llm ollama
|
||||||
quicommit config set-ollama --url http://localhost:11434 --model llama2
|
quicommit config set-llm ollama --url http://localhost:11434 --model llama2
|
||||||
|
|
||||||
# 配置OpenAI
|
# 配置OpenAI
|
||||||
quicommit config set-llm openai
|
quicommit config set-llm openai
|
||||||
quicommit config set-openai-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
# 配置Anthropic Claude
|
# 配置Anthropic Claude
|
||||||
quicommit config set-llm anthropic
|
quicommit config set-llm anthropic
|
||||||
quicommit config set-anthropic-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
# 配置Kimi
|
# 配置Kimi
|
||||||
quicommit config set-llm kimi
|
quicommit config set-llm kimi
|
||||||
quicommit config set-kimi-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||||
|
|
||||||
# 配置DeepSeek
|
# 配置DeepSeek
|
||||||
quicommit config set-llm deepseek
|
quicommit config set-llm deepseek
|
||||||
quicommit config set-deepseek-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||||
|
|
||||||
# 配置OpenRouter
|
# 配置OpenRouter
|
||||||
quicommit config set-llm openrouter
|
quicommit config set-llm openrouter
|
||||||
quicommit config set-openrouter-key YOUR_API_KEY
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
quicommit config set-llm openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||||
|
|
||||||
|
# 设置提交格式
|
||||||
|
quicommit config set-commit-format conventional
|
||||||
|
|
||||||
|
# 设置版本前缀
|
||||||
|
quicommit config set-version-prefix v
|
||||||
|
|
||||||
|
# 设置变更日志路径
|
||||||
|
quicommit config set-changelog-path CHANGELOG.md
|
||||||
|
|
||||||
|
# 设置输出语言(zh, en, ja, ko, es, fr, de)
|
||||||
|
quicommit config set-language zh
|
||||||
|
|
||||||
|
# 设置保持提交类型为英文
|
||||||
|
quicommit config set-keep-types-english true
|
||||||
|
|
||||||
|
# 设置保持变更日志类型为英文
|
||||||
|
quicommit config set-keep-changelog-types-english true
|
||||||
|
|
||||||
# 测试LLM连接
|
# 测试LLM连接
|
||||||
quicommit config test-llm
|
quicommit config test-llm
|
||||||
|
|
||||||
|
# 检查密钥环可用性
|
||||||
|
quicommit config check-keyring
|
||||||
|
|
||||||
|
# 显示配置文件路径
|
||||||
|
quicommit config path
|
||||||
|
|
||||||
|
# 导出配置(支持加密)
|
||||||
|
quicommit config export -o config-backup.toml
|
||||||
|
quicommit config export -o config-backup.enc --password
|
||||||
|
|
||||||
|
# 导入配置
|
||||||
|
quicommit config import -i config-backup.toml
|
||||||
|
quicommit config import -i config-backup.enc --password
|
||||||
|
|
||||||
|
# 重置配置为默认值
|
||||||
|
quicommit config reset --force
|
||||||
```
|
```
|
||||||
|
|
||||||
## 命令参考
|
## 命令参考
|
||||||
@@ -139,17 +243,23 @@ quicommit config test-llm
|
|||||||
|
|
||||||
| 选项 | 说明 |
|
| 选项 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `-t, --commit-type` | 提交类型(feat、fix等) |
|
| `--commit-type` | 提交类型(feat、fix等) |
|
||||||
| `-s, --scope` | 提交范围 |
|
| `-s, --scope` | 提交范围 |
|
||||||
| `-m, --message` | 提交描述 |
|
| `-m, --message` | 提交描述 |
|
||||||
| `--body` | 提交正文 |
|
| `--body` | 提交正文 |
|
||||||
| `--breaking` | 标记为破坏性变更 |
|
| `-b, --breaking` | 标记为破坏性变更 |
|
||||||
|
| `-d, --date` | 使用日期格式的提交信息 |
|
||||||
| `--manual` | 手动输入,跳过AI生成 |
|
| `--manual` | 手动输入,跳过AI生成 |
|
||||||
| `-a, --all` | 暂存所有更改 |
|
| `-a, --all` | 暂存所有更改 |
|
||||||
| `-S, --sign` | GPG签名提交 |
|
| `-S, --sign` | GPG签名提交 |
|
||||||
| `--amend` | 修改上一次提交 |
|
| `--amend` | 修改上一次提交 |
|
||||||
| `--dry-run` | 试运行,不实际提交 |
|
| `--dry-run` | 试运行,不实际提交 |
|
||||||
|
| `--conventional` | 使用Conventional Commits格式 |
|
||||||
|
| `--commitlint` | 使用commitlint格式 |
|
||||||
|
| `--no-verify` | 不验证提交信息 |
|
||||||
| `-y, --yes` | 跳过确认提示 |
|
| `-y, --yes` | 跳过确认提示 |
|
||||||
|
| `--push` | 提交后推送到远程 |
|
||||||
|
| `--remote` | 指定远程仓库(默认:origin) |
|
||||||
|
|
||||||
### tag命令选项
|
### tag命令选项
|
||||||
|
|
||||||
@@ -160,15 +270,36 @@ quicommit config test-llm
|
|||||||
| `-m, --message` | 标签信息 |
|
| `-m, --message` | 标签信息 |
|
||||||
| `-g, --generate` | AI生成标签信息 |
|
| `-g, --generate` | AI生成标签信息 |
|
||||||
| `-S, --sign` | GPG签名标签 |
|
| `-S, --sign` | GPG签名标签 |
|
||||||
| `--lightweight` | 创建轻量标签 |
|
| `-l, --lightweight` | 创建轻量标签 |
|
||||||
| `--push` | 推送到远程 |
|
| `-f, --force` | 强制覆盖已存在的标签 |
|
||||||
|
| `-p, --push` | 推送到远程 |
|
||||||
|
| `-r, --remote` | 指定远程仓库(默认:origin) |
|
||||||
|
| `--dry-run` | 试运行 |
|
||||||
|
| `-y, --yes` | 跳过确认提示 |
|
||||||
|
|
||||||
|
### changelog命令选项
|
||||||
|
|
||||||
|
| 选项 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `-o, --output` | 输出文件路径 |
|
||||||
|
| `-v, --version` | 为特定版本生成 |
|
||||||
|
| `-f, --from` | 从指定标签生成 |
|
||||||
|
| `-t, --to` | 生成到指定引用(默认:HEAD) |
|
||||||
|
| `-i, --init` | 初始化新的变更日志文件 |
|
||||||
|
| `-g, --generate` | AI生成变更日志 |
|
||||||
|
| `--prepend` | 添加到现有变更日志开头 |
|
||||||
|
| `--include-hashes` | 包含提交哈希 |
|
||||||
|
| `--include-authors` | 包含作者信息 |
|
||||||
|
| `--format` | 格式(keep-a-changelog、github-releases) |
|
||||||
|
| `--dry-run` | 试运行(输出到stdout) |
|
||||||
| `-y, --yes` | 跳过确认提示 |
|
| `-y, --yes` | 跳过确认提示 |
|
||||||
|
|
||||||
## 配置文件
|
## 配置文件
|
||||||
|
|
||||||
配置文件位置:
|
配置文件位置:
|
||||||
- Linux/macOS: `~/.config/quicommit/config.toml`
|
- Linux: `~/.config/quicommit/config.toml`
|
||||||
- Windows: `%APPDATA%\quicommit\config.toml`
|
- macOS: `~/Library/Application Support/quicommit/config.toml`
|
||||||
|
- Windows: `%APPDATA%\quicommit/config.toml`
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
version = "1"
|
version = "1"
|
||||||
@@ -176,15 +307,27 @@ default_profile = "personal"
|
|||||||
|
|
||||||
[profiles.personal]
|
[profiles.personal]
|
||||||
name = "personal"
|
name = "personal"
|
||||||
user_name = "John Doe"
|
user_name = "Your Name"
|
||||||
user_email = "john@example.com"
|
user_email = "your.email@example.com"
|
||||||
|
description = "个人项目"
|
||||||
|
is_work = false
|
||||||
|
|
||||||
[profiles.work]
|
[profiles.work]
|
||||||
name = "work"
|
name = "work"
|
||||||
user_name = "John Doe"
|
user_name = "Your Name"
|
||||||
user_email = "john@company.com"
|
user_email = "your.name@company.com"
|
||||||
|
description = "工作项目"
|
||||||
is_work = true
|
is_work = true
|
||||||
organization = "Acme Corp"
|
organization = "Your Company"
|
||||||
|
|
||||||
|
[profiles.work.ssh]
|
||||||
|
private_key_path = "/home/user/.ssh/id_rsa_work"
|
||||||
|
agent_forwarding = true
|
||||||
|
|
||||||
|
[profiles.work.gpg]
|
||||||
|
key_id = "YOUR_GPG_KEY_ID"
|
||||||
|
program = "gpg"
|
||||||
|
use_agent = true
|
||||||
|
|
||||||
[llm]
|
[llm]
|
||||||
provider = "ollama"
|
provider = "ollama"
|
||||||
@@ -200,19 +343,50 @@ model = "llama2"
|
|||||||
model = "gpt-4"
|
model = "gpt-4"
|
||||||
base_url = "https://api.openai.com/v1"
|
base_url = "https://api.openai.com/v1"
|
||||||
|
|
||||||
|
[llm.anthropic]
|
||||||
|
model = "claude-3-sonnet-20240229"
|
||||||
|
|
||||||
|
[llm.kimi]
|
||||||
|
model = "moonshot-v1-8k"
|
||||||
|
|
||||||
|
[llm.deepseek]
|
||||||
|
model = "deepseek-chat"
|
||||||
|
|
||||||
|
[llm.openrouter]
|
||||||
|
model = "openai/gpt-4"
|
||||||
|
|
||||||
[commit]
|
[commit]
|
||||||
format = "conventional"
|
format = "conventional"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
allow_empty = false
|
||||||
|
gpg_sign = false
|
||||||
max_subject_length = 100
|
max_subject_length = 100
|
||||||
|
require_scope = false
|
||||||
|
require_body = false
|
||||||
|
body_required_types = ["feat", "fix"]
|
||||||
|
|
||||||
[tag]
|
[tag]
|
||||||
version_prefix = "v"
|
version_prefix = "v"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
gpg_sign = false
|
||||||
|
include_changelog = true
|
||||||
|
|
||||||
[changelog]
|
[changelog]
|
||||||
path = "CHANGELOG.md"
|
path = "CHANGELOG.md"
|
||||||
auto_generate = true
|
auto_generate = true
|
||||||
|
format = "keep-a-changelog"
|
||||||
|
include_hashes = false
|
||||||
|
include_authors = false
|
||||||
group_by_type = true
|
group_by_type = true
|
||||||
|
|
||||||
|
[theme]
|
||||||
|
colors = true
|
||||||
|
icons = true
|
||||||
|
date_format = "%Y-%m-%d"
|
||||||
|
|
||||||
|
[repo_profiles]
|
||||||
|
"/path/to/work/project" = "work"
|
||||||
|
"/path/to/personal/project" = "personal"
|
||||||
```
|
```
|
||||||
|
|
||||||
## 环境变量
|
## 环境变量
|
||||||
@@ -229,14 +403,46 @@ group_by_type = true
|
|||||||
# 查看当前配置
|
# 查看当前配置
|
||||||
quicommit config list
|
quicommit config list
|
||||||
|
|
||||||
|
# 显示配置详情
|
||||||
|
quicommit config show
|
||||||
|
|
||||||
|
# 编辑配置文件
|
||||||
|
quicommit config edit
|
||||||
|
|
||||||
|
# 设置配置值
|
||||||
|
quicommit config set llm.provider ollama
|
||||||
|
|
||||||
|
# 获取配置值
|
||||||
|
quicommit config get llm.provider
|
||||||
|
|
||||||
|
# 设置API密钥(存储在系统密钥环中)
|
||||||
|
quicommit config set-api-key YOUR_API_KEY
|
||||||
|
|
||||||
|
# 从密钥环删除API密钥
|
||||||
|
quicommit config delete-api-key
|
||||||
|
|
||||||
# 测试LLM连接
|
# 测试LLM连接
|
||||||
quicommit config test-llm
|
quicommit config test-llm
|
||||||
|
|
||||||
# 列出可用模型
|
# 列出可用模型
|
||||||
quicommit config list-models
|
quicommit config list-models
|
||||||
|
|
||||||
# 编辑配置文件
|
# 检查密钥环可用性
|
||||||
quicommit config edit
|
quicommit config check-keyring
|
||||||
|
|
||||||
|
# 显示配置文件路径
|
||||||
|
quicommit config path
|
||||||
|
|
||||||
|
# 导出配置(支持加密)
|
||||||
|
quicommit config export -o config-backup.toml
|
||||||
|
quicommit config export -o config-backup.enc --password
|
||||||
|
|
||||||
|
# 导入配置
|
||||||
|
quicommit config import -i config-backup.toml
|
||||||
|
quicommit config import -i config-backup.enc --password
|
||||||
|
|
||||||
|
# 重置配置
|
||||||
|
quicommit config reset --force
|
||||||
```
|
```
|
||||||
|
|
||||||
## 贡献
|
## 贡献
|
||||||
@@ -255,8 +461,8 @@ quicommit config edit
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 克隆仓库
|
# 克隆仓库
|
||||||
git clone https://github.com/YOUR_USERNAME/quicommit.git
|
git clone https://git.lyz.one/SidneyZhang/QuiCommit.git
|
||||||
cd quicommit
|
cd QuiCommit
|
||||||
|
|
||||||
# 安装依赖
|
# 安装依赖
|
||||||
cargo fetch
|
cargo fetch
|
||||||
@@ -284,11 +490,36 @@ cargo fmt --check
|
|||||||
```
|
```
|
||||||
src/
|
src/
|
||||||
├── commands/ # CLI命令实现
|
├── commands/ # CLI命令实现
|
||||||
|
│ ├── commit.rs
|
||||||
|
│ ├── tag.rs
|
||||||
|
│ ├── changelog.rs
|
||||||
|
│ ├── profile.rs
|
||||||
|
│ ├── config.rs
|
||||||
|
│ └── init.rs
|
||||||
├── config/ # 配置管理
|
├── config/ # 配置管理
|
||||||
|
│ ├── manager.rs
|
||||||
|
│ └── profile.rs
|
||||||
├── generator/ # AI内容生成
|
├── generator/ # AI内容生成
|
||||||
├── git/ # Git操作封装
|
├── git/ # Git操作封装
|
||||||
|
│ ├── commit.rs
|
||||||
|
│ ├── tag.rs
|
||||||
|
│ └── changelog.rs
|
||||||
├── llm/ # LLM提供商实现
|
├── llm/ # LLM提供商实现
|
||||||
└── utils/ # 工具函数
|
│ ├── ollama.rs
|
||||||
|
│ ├── openai.rs
|
||||||
|
│ ├── anthropic.rs
|
||||||
|
│ ├── kimi.rs
|
||||||
|
│ ├── deepseek.rs
|
||||||
|
│ └── openrouter.rs
|
||||||
|
├── i18n/ # 国际化支持
|
||||||
|
│ ├── messages.rs
|
||||||
|
│ └── translator.rs
|
||||||
|
├── utils/ # 工具函数
|
||||||
|
│ ├── validators.rs
|
||||||
|
│ ├── formatter.rs
|
||||||
|
│ ├── crypto.rs
|
||||||
|
│ └── editor.rs
|
||||||
|
└── main.rs # 程序入口
|
||||||
```
|
```
|
||||||
|
|
||||||
## 许可证
|
## 许可证
|
||||||
|
|||||||
@@ -1,24 +1,26 @@
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{Result, bail};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use dialoguer::{Confirm, Input};
|
use dialoguer::{Confirm, Input};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::config::manager::ConfigManager;
|
use crate::config::{Language, manager::ConfigManager};
|
||||||
use crate::generator::ContentGenerator;
|
use crate::generator::ContentGenerator;
|
||||||
use crate::git::find_repo;
|
use crate::git::find_repo;
|
||||||
use crate::git::{changelog::*, CommitInfo, GitRepo};
|
use crate::git::{CommitInfo, changelog::*};
|
||||||
|
use crate::i18n::{Messages, translate_changelog_category};
|
||||||
|
|
||||||
/// Generate changelog
|
/// Generate changelog
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
#[command(disable_version_flag = true, disable_help_flag = false)]
|
||||||
pub struct ChangelogCommand {
|
pub struct ChangelogCommand {
|
||||||
/// Output file path
|
/// Output file path
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
output: Option<PathBuf>,
|
output: Option<PathBuf>,
|
||||||
|
|
||||||
/// Version to generate changelog for
|
/// Version to generate changelog for
|
||||||
#[arg(short, long)]
|
#[arg(long)]
|
||||||
version: Option<String>,
|
version: Option<String>,
|
||||||
|
|
||||||
/// Generate from specific tag
|
/// Generate from specific tag
|
||||||
@@ -37,10 +39,6 @@ pub struct ChangelogCommand {
|
|||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
generate: bool,
|
generate: bool,
|
||||||
|
|
||||||
/// Prepend to existing changelog
|
|
||||||
#[arg(short, long)]
|
|
||||||
prepend: bool,
|
|
||||||
|
|
||||||
/// Include commit hashes
|
/// Include commit hashes
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
include_hashes: bool,
|
include_hashes: bool,
|
||||||
@@ -50,38 +48,50 @@ pub struct ChangelogCommand {
|
|||||||
include_authors: bool,
|
include_authors: bool,
|
||||||
|
|
||||||
/// Format (keep-a-changelog, github-releases)
|
/// Format (keep-a-changelog, github-releases)
|
||||||
#[arg(short, long)]
|
#[arg(long)]
|
||||||
format: Option<String>,
|
format: Option<String>,
|
||||||
|
|
||||||
/// Dry run (output to stdout)
|
/// Dry run (output to stdout)
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
|
|
||||||
|
/// Enable thinking mode for this changelog (override config)
|
||||||
|
#[arg(long)]
|
||||||
|
think: bool,
|
||||||
|
|
||||||
/// Skip interactive prompts
|
/// Skip interactive prompts
|
||||||
#[arg(short = 'y', long)]
|
#[arg(short = 'y', long)]
|
||||||
yes: bool,
|
yes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChangelogCommand {
|
impl ChangelogCommand {
|
||||||
pub async fn execute(&self) -> Result<()> {
|
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||||
let manager = ConfigManager::new()?;
|
let manager = if let Some(ref path) = config_path {
|
||||||
|
ConfigManager::with_path(path)?
|
||||||
|
} else {
|
||||||
|
ConfigManager::new()?
|
||||||
|
};
|
||||||
let config = manager.config();
|
let config = manager.config();
|
||||||
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
let messages = Messages::new(language);
|
||||||
|
|
||||||
// Initialize changelog if requested
|
// Initialize changelog if requested
|
||||||
if self.init {
|
if self.init {
|
||||||
let path = self.output.as_ref()
|
let path = self
|
||||||
.map(|p| p.clone())
|
.output
|
||||||
|
.clone()
|
||||||
.unwrap_or_else(|| PathBuf::from(&config.changelog.path));
|
.unwrap_or_else(|| PathBuf::from(&config.changelog.path));
|
||||||
|
|
||||||
init_changelog(&path)?;
|
init_changelog(&path)?;
|
||||||
println!("{} Initialized changelog at {:?}", "✓".green(), path);
|
println!("{}", messages.initialized_changelog(&format!("{:?}", path)));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine output path
|
// Determine output path
|
||||||
let output_path = self.output.as_ref()
|
let output_path = self
|
||||||
.map(|p| p.clone())
|
.output
|
||||||
|
.clone()
|
||||||
.unwrap_or_else(|| PathBuf::from(&config.changelog.path));
|
.unwrap_or_else(|| PathBuf::from(&config.changelog.path));
|
||||||
|
|
||||||
// Determine format
|
// Determine format
|
||||||
@@ -90,7 +100,10 @@ impl ChangelogCommand {
|
|||||||
Some("keep") | Some("keep-a-changelog") => ChangelogFormat::KeepAChangelog,
|
Some("keep") | Some("keep-a-changelog") => ChangelogFormat::KeepAChangelog,
|
||||||
Some("custom") => ChangelogFormat::Custom,
|
Some("custom") => ChangelogFormat::Custom,
|
||||||
None => ChangelogFormat::KeepAChangelog,
|
None => ChangelogFormat::KeepAChangelog,
|
||||||
Some(f) => bail!("Unknown format: {}. Use: keep-a-changelog, github-releases", f),
|
Some(f) => bail!(
|
||||||
|
"Unknown format: {}. Use: keep-a-changelog, github-releases",
|
||||||
|
f
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get version
|
// Get version
|
||||||
@@ -98,28 +111,28 @@ impl ChangelogCommand {
|
|||||||
v.clone()
|
v.clone()
|
||||||
} else if !self.yes {
|
} else if !self.yes {
|
||||||
Input::new()
|
Input::new()
|
||||||
.with_prompt("Version")
|
.with_prompt(messages.version())
|
||||||
.default("Unreleased".to_string())
|
.default(messages.unreleased().to_string())
|
||||||
.interact_text()?
|
.interact_text()?
|
||||||
} else {
|
} else {
|
||||||
"Unreleased".to_string()
|
messages.unreleased().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get commits
|
// Get commits
|
||||||
println!("{} Fetching commits...", "→".blue());
|
println!("{}", messages.fetching_commits());
|
||||||
let commits = generate_from_history(&repo, self.from.as_deref(), Some(&self.to))?;
|
let commits = generate_from_history(&repo, self.from.as_deref(), Some(&self.to))?;
|
||||||
|
|
||||||
if commits.is_empty() {
|
if commits.is_empty() {
|
||||||
bail!("No commits found in the specified range");
|
bail!("{}", messages.no_commits_found());
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{} Found {} commits", "✓".green(), commits.len());
|
println!("{}", messages.found_commits(commits.len()));
|
||||||
|
|
||||||
// Generate changelog
|
// Generate changelog
|
||||||
let changelog = if self.generate || (config.changelog.auto_generate && !self.yes) {
|
let changelog = if self.generate || (config.changelog.auto_generate && !self.yes) {
|
||||||
self.generate_with_ai(&repo, &version, &commits).await?
|
self.generate_with_ai(&version, &commits, &messages).await?
|
||||||
} else {
|
} else {
|
||||||
self.generate_with_template(format, &version, &commits)?
|
self.generate_with_template(format, &version, &commits, language)?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Output or write
|
// Output or write
|
||||||
@@ -133,7 +146,7 @@ impl ChangelogCommand {
|
|||||||
// Preview
|
// Preview
|
||||||
if !self.yes {
|
if !self.yes {
|
||||||
println!("\n{}", "─".repeat(60));
|
println!("\n{}", "─".repeat(60));
|
||||||
println!("{}", "Changelog preview:".bold());
|
println!("{}", messages.changelog_preview().bold());
|
||||||
println!("{}", "─".repeat(60));
|
println!("{}", "─".repeat(60));
|
||||||
// Show first 20 lines
|
// Show first 20 lines
|
||||||
let preview: String = changelog.lines().take(20).collect::<Vec<_>>().join("\n");
|
let preview: String = changelog.lines().take(20).collect::<Vec<_>>().join("\n");
|
||||||
@@ -144,43 +157,70 @@ impl ChangelogCommand {
|
|||||||
println!("{}", "─".repeat(60));
|
println!("{}", "─".repeat(60));
|
||||||
|
|
||||||
let confirm = Confirm::new()
|
let confirm = Confirm::new()
|
||||||
.with_prompt(&format!("Write to {:?}?", output_path))
|
.with_prompt(messages.write_to_file(&format!("{:?}", output_path)))
|
||||||
.default(true)
|
.default(true)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
if !confirm {
|
if !confirm {
|
||||||
println!("{}", "Cancelled.".yellow());
|
println!("{}", messages.cancelled().yellow());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to file
|
// Write to file (always prepend to preserve history)
|
||||||
if self.prepend && output_path.exists() {
|
if output_path.exists() {
|
||||||
let existing = std::fs::read_to_string(&output_path)?;
|
let existing = std::fs::read_to_string(&output_path)?;
|
||||||
let new_content = format!("{}\n{}", changelog, existing);
|
let new_content = if existing.is_empty() {
|
||||||
std::fs::write(&output_path, new_content)?;
|
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||||
|
} else if existing.starts_with(CHANGELOG_HEADER) {
|
||||||
|
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||||
|
} else if existing.starts_with("# Changelog") {
|
||||||
|
let lines: Vec<&str> = existing.lines().collect();
|
||||||
|
let mut header_end = 0;
|
||||||
|
|
||||||
|
for (i, line) in lines.iter().enumerate() {
|
||||||
|
if i == 0 && line.starts_with('#') {
|
||||||
|
header_end = i + 1;
|
||||||
|
} else if line.trim().is_empty() {
|
||||||
|
header_end = i + 1;
|
||||||
} else {
|
} else {
|
||||||
std::fs::write(&output_path, changelog)?;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{} Changelog written to {:?}", "✓".green(), output_path);
|
let header = lines[..header_end].join("\n");
|
||||||
|
let rest = lines[header_end..].join("\n");
|
||||||
|
|
||||||
|
format!("{}\n{}\n{}", header, changelog, rest)
|
||||||
|
} else {
|
||||||
|
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||||
|
};
|
||||||
|
std::fs::write(&output_path, new_content)?;
|
||||||
|
} else {
|
||||||
|
let content = format!("{}{}", CHANGELOG_HEADER, changelog);
|
||||||
|
std::fs::write(&output_path, content)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{} {:?}", messages.changelog_written(), output_path);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_with_ai(
|
async fn generate_with_ai(
|
||||||
&self,
|
&self,
|
||||||
repo: &GitRepo,
|
|
||||||
version: &str,
|
version: &str,
|
||||||
commits: &[CommitInfo],
|
commits: &[CommitInfo],
|
||||||
|
messages: &Messages,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let manager = ConfigManager::new()?;
|
let manager = ConfigManager::new()?;
|
||||||
let config = manager.config();
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
|
||||||
println!("{} AI is generating changelog...", "🤖");
|
println!("{}", messages.ai_generating_changelog());
|
||||||
|
|
||||||
let generator = ContentGenerator::new(&config.llm).await?;
|
let generator = ContentGenerator::new_with_think(&manager, self.think).await?;
|
||||||
generator.generate_changelog_entry(version, commits).await
|
generator
|
||||||
|
.generate_changelog_entry(version, commits, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_with_template(
|
fn generate_with_template(
|
||||||
@@ -188,12 +228,43 @@ impl ChangelogCommand {
|
|||||||
format: ChangelogFormat,
|
format: ChangelogFormat,
|
||||||
version: &str,
|
version: &str,
|
||||||
commits: &[CommitInfo],
|
commits: &[CommitInfo],
|
||||||
|
language: Language,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
|
let manager = ConfigManager::new()?;
|
||||||
|
|
||||||
let generator = ChangelogGenerator::new()
|
let generator = ChangelogGenerator::new()
|
||||||
.format(format)
|
.format(format)
|
||||||
.include_hashes(self.include_hashes)
|
.include_hashes(self.include_hashes)
|
||||||
.include_authors(self.include_authors);
|
.include_authors(self.include_authors);
|
||||||
|
|
||||||
generator.generate(version, Utc::now(), commits)
|
let changelog = generator.generate(version, Utc::now(), commits)?;
|
||||||
|
|
||||||
|
// Translate changelog categories if configured
|
||||||
|
if !manager.keep_changelog_types_english() {
|
||||||
|
Ok(self.translate_changelog_categories(&changelog, language))
|
||||||
|
} else {
|
||||||
|
Ok(changelog)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_categories(&self, changelog: &str, language: Language) -> String {
|
||||||
|
changelog
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
if line.starts_with("## ") || line.starts_with("### ") {
|
||||||
|
let category = line.trim_start_matches("## ").trim_start_matches("### ");
|
||||||
|
let translated_category =
|
||||||
|
translate_changelog_category(category, language, false);
|
||||||
|
if line.starts_with("## ") {
|
||||||
|
format!("## {}", translated_category)
|
||||||
|
} else {
|
||||||
|
format!("### {}", translated_category)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line.to_string()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use dialoguer::{Confirm, Input, Select};
|
use dialoguer::{Confirm, Input, Select};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::config::manager::ConfigManager;
|
|
||||||
use crate::config::CommitFormat;
|
use crate::config::CommitFormat;
|
||||||
|
use crate::config::{Language, manager::ConfigManager};
|
||||||
use crate::generator::ContentGenerator;
|
use crate::generator::ContentGenerator;
|
||||||
use crate::git::{find_repo, GitRepo};
|
|
||||||
use crate::git::commit::{CommitBuilder, create_date_commit_message};
|
use crate::git::commit::{CommitBuilder, create_date_commit_message};
|
||||||
|
use crate::git::{GitRepo, find_repo};
|
||||||
|
use crate::i18n::Messages;
|
||||||
use crate::utils::validators::get_commit_types;
|
use crate::utils::validators::get_commit_types;
|
||||||
|
|
||||||
/// Generate and execute conventional commits
|
/// Generate and execute conventional commits
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct CommitCommand {
|
pub struct CommitCommand {
|
||||||
/// Commit type
|
/// Commit type
|
||||||
#[arg(short, long)]
|
#[arg(long)]
|
||||||
commit_type: Option<String>,
|
commit_type: Option<String>,
|
||||||
|
|
||||||
/// Commit scope
|
/// Commit scope
|
||||||
@@ -38,7 +40,7 @@ pub struct CommitCommand {
|
|||||||
date: bool,
|
date: bool,
|
||||||
|
|
||||||
/// Manual input (skip AI generation)
|
/// Manual input (skip AI generation)
|
||||||
#[arg(short, long)]
|
#[arg(long)]
|
||||||
manual: bool,
|
manual: bool,
|
||||||
|
|
||||||
/// Sign the commit
|
/// Sign the commit
|
||||||
@@ -69,26 +71,44 @@ pub struct CommitCommand {
|
|||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
no_verify: bool,
|
no_verify: bool,
|
||||||
|
|
||||||
|
/// Enable thinking mode for this commit (override config)
|
||||||
|
#[arg(short = 't', long)]
|
||||||
|
think: bool,
|
||||||
|
|
||||||
/// Skip interactive prompts
|
/// Skip interactive prompts
|
||||||
#[arg(short = 'y', long)]
|
#[arg(short = 'y', long)]
|
||||||
yes: bool,
|
yes: bool,
|
||||||
|
|
||||||
|
/// Push after committing
|
||||||
|
#[arg(long)]
|
||||||
|
push: bool,
|
||||||
|
|
||||||
|
/// Remote to push to
|
||||||
|
#[arg(long, default_value = "origin")]
|
||||||
|
remote: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommitCommand {
|
impl CommitCommand {
|
||||||
pub async fn execute(&self) -> Result<()> {
|
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||||
// Find git repository
|
// Find git repository
|
||||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||||
|
|
||||||
|
// Load configuration
|
||||||
|
let manager = if let Some(ref path) = config_path {
|
||||||
|
ConfigManager::with_path(path)?
|
||||||
|
} else {
|
||||||
|
ConfigManager::new()?
|
||||||
|
};
|
||||||
|
let config = manager.config();
|
||||||
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
let messages = Messages::new(language);
|
||||||
|
|
||||||
// Check for changes
|
// Check for changes
|
||||||
let status = repo.status_summary()?;
|
let status = repo.status_summary()?;
|
||||||
if status.clean && !self.amend {
|
if status.clean && !self.amend {
|
||||||
bail!("No changes to commit. Working tree is clean.");
|
bail!("{}", messages.no_changes());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load configuration
|
|
||||||
let manager = ConfigManager::new()?;
|
|
||||||
let config = manager.config();
|
|
||||||
|
|
||||||
// Determine commit format
|
// Determine commit format
|
||||||
let format = if self.conventional {
|
let format = if self.conventional {
|
||||||
CommitFormat::Conventional
|
CommitFormat::Conventional
|
||||||
@@ -98,10 +118,23 @@ impl CommitCommand {
|
|||||||
config.commit.format
|
config.commit.format
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Auto-add if no files are staged and there are unstaged/untracked changes
|
||||||
|
if status.staged == 0 && (status.unstaged > 0 || status.untracked > 0) && !self.all {
|
||||||
|
println!("{}", messages.auto_stage_changes().yellow());
|
||||||
|
repo.stage_all()?;
|
||||||
|
println!("{}", messages.staged_all().green());
|
||||||
|
|
||||||
|
// Re-check status after staging to ensure changes are detected
|
||||||
|
let new_status = repo.status_summary()?;
|
||||||
|
if new_status.staged == 0 {
|
||||||
|
bail!("Failed to stage changes. Please try running 'git add -A' manually.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Stage all if requested
|
// Stage all if requested
|
||||||
if self.all {
|
if self.all {
|
||||||
repo.stage_all()?;
|
repo.stage_all()?;
|
||||||
println!("{}", "✓ Staged all changes".green());
|
println!("{}", messages.staged_all().green());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate or build commit message
|
// Generate or build commit message
|
||||||
@@ -111,12 +144,12 @@ impl CommitCommand {
|
|||||||
} else if self.manual || self.message.is_some() {
|
} else if self.manual || self.message.is_some() {
|
||||||
// Manual commit
|
// Manual commit
|
||||||
self.create_manual_commit(format)?
|
self.create_manual_commit(format)?
|
||||||
} else if config.commit.auto_generate && !self.yes {
|
} else if config.commit.auto_generate {
|
||||||
// AI-generated commit
|
// AI-generated commit
|
||||||
self.generate_commit(&repo, format).await?
|
self.generate_commit(&repo, format, &messages).await?
|
||||||
} else {
|
} else {
|
||||||
// Interactive commit creation
|
// Interactive commit creation
|
||||||
self.create_interactive_commit(format).await?
|
self.create_interactive_commit(format, &messages).await?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate message
|
// Validate message
|
||||||
@@ -132,39 +165,74 @@ impl CommitCommand {
|
|||||||
// Show commit preview
|
// Show commit preview
|
||||||
if !self.yes {
|
if !self.yes {
|
||||||
println!("\n{}", "─".repeat(60));
|
println!("\n{}", "─".repeat(60));
|
||||||
println!("{}", "Commit preview:".bold());
|
println!("{}", messages.commit_preview().bold());
|
||||||
println!("{}", "─".repeat(60));
|
println!("{}", "─".repeat(60));
|
||||||
println!("{}", commit_message);
|
println!("{}", commit_message);
|
||||||
println!("{}", "─".repeat(60));
|
println!("{}", "─".repeat(60));
|
||||||
|
|
||||||
let confirm = Confirm::new()
|
let confirm = Confirm::new()
|
||||||
.with_prompt("Do you want to proceed with this commit?")
|
.with_prompt(messages.proceed_commit())
|
||||||
.default(true)
|
.default(true)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
if !confirm {
|
if !confirm {
|
||||||
println!("{}", "Commit cancelled.".yellow());
|
println!("{}", messages.commit_cancelled().yellow());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.dry_run {
|
|
||||||
println!("\n{}", "Dry run - commit not created.".yellow());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute commit
|
|
||||||
let result = if self.amend {
|
let result = if self.amend {
|
||||||
|
if self.dry_run {
|
||||||
|
println!(
|
||||||
|
"\n{} {}",
|
||||||
|
messages.dry_run(),
|
||||||
|
"- commit not amended.".yellow()
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
self.amend_commit(&repo, &commit_message)?;
|
self.amend_commit(&repo, &commit_message)?;
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(repo.commit(&commit_message, self.sign)?)
|
if self.dry_run {
|
||||||
|
println!(
|
||||||
|
"\n{} {}",
|
||||||
|
messages.dry_run(),
|
||||||
|
"- commit not created.".yellow()
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
CommitBuilder::new()
|
||||||
|
.message(&commit_message)
|
||||||
|
.sign(self.sign)
|
||||||
|
.execute(&repo)?
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(commit_oid) = result {
|
if let Some(commit_oid) = result {
|
||||||
println!("{} {}", "✓ Created commit".green().bold(), commit_oid.to_string()[..8].to_string().cyan());
|
println!(
|
||||||
|
"{} {}",
|
||||||
|
messages.commit_created().green().bold(),
|
||||||
|
commit_oid.to_string()[..8].to_string().cyan()
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("{} {}", "✓ Amended commit".green().bold(), "successfully");
|
println!("{} successfully", messages.commit_amended().green().bold());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push after commit if requested or ask user
|
||||||
|
if self.push {
|
||||||
|
println!("\n{}", messages.pushing_commit(&self.remote));
|
||||||
|
repo.push(&self.remote, "HEAD")?;
|
||||||
|
println!("{}", messages.pushed_commit(&self.remote));
|
||||||
|
} else if !self.yes && !self.dry_run {
|
||||||
|
let should_push = Confirm::new()
|
||||||
|
.with_prompt(messages.push_after_commit())
|
||||||
|
.default(false)
|
||||||
|
.interact()?;
|
||||||
|
|
||||||
|
if should_push {
|
||||||
|
println!("\n{}", messages.pushing_commit(&self.remote));
|
||||||
|
repo.push(&self.remote, "HEAD")?;
|
||||||
|
println!("{}", messages.pushed_commit(&self.remote));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -176,11 +244,22 @@ impl CommitCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_manual_commit(&self, format: CommitFormat) -> Result<String> {
|
fn create_manual_commit(&self, format: CommitFormat) -> Result<String> {
|
||||||
let commit_type = self.commit_type.clone()
|
let description = self.message.clone().ok_or_else(|| {
|
||||||
.ok_or_else(|| anyhow::anyhow!("Commit type required for manual commit. Use -t <type>"))?;
|
anyhow::anyhow!("Description required for manual commit. Use -m <message>")
|
||||||
|
})?;
|
||||||
|
|
||||||
let description = self.message.clone()
|
// Try to extract commit type from message if not provided
|
||||||
.ok_or_else(|| anyhow::anyhow!("Description required for manual commit. Use -m <message>"))?;
|
let commit_type = if let Some(ref ct) = self.commit_type {
|
||||||
|
ct.clone()
|
||||||
|
} else {
|
||||||
|
// Parse from conventional commit format: "type: description"
|
||||||
|
description
|
||||||
|
.split(':')
|
||||||
|
.next()
|
||||||
|
.unwrap_or("feat")
|
||||||
|
.trim()
|
||||||
|
.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
let builder = CommitBuilder::new()
|
let builder = CommitBuilder::new()
|
||||||
.commit_type(commit_type)
|
.commit_type(commit_type)
|
||||||
@@ -193,61 +272,75 @@ impl CommitCommand {
|
|||||||
builder.build_message()
|
builder.build_message()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_commit(&self, repo: &GitRepo, format: CommitFormat) -> Result<String> {
|
async fn generate_commit(
|
||||||
|
&self,
|
||||||
|
repo: &GitRepo,
|
||||||
|
format: CommitFormat,
|
||||||
|
messages: &Messages,
|
||||||
|
) -> Result<String> {
|
||||||
let manager = ConfigManager::new()?;
|
let manager = ConfigManager::new()?;
|
||||||
let config = manager.config();
|
|
||||||
|
|
||||||
// Check if LLM is configured
|
let generator = ContentGenerator::new_with_think(&manager, self.think)
|
||||||
let generator = ContentGenerator::new(&config.llm).await
|
.await
|
||||||
.context("Failed to initialize LLM. Use --manual for manual commit.")?;
|
.context("Failed to initialize LLM. Use --manual for manual commit.")?;
|
||||||
|
|
||||||
println!("{} AI is analyzing your changes...", "🤖".to_string());
|
println!("{}", messages.ai_analyzing());
|
||||||
|
|
||||||
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
|
||||||
let generated = if self.yes {
|
let generated = if self.yes {
|
||||||
generator.generate_commit_from_repo(repo, format).await?
|
generator
|
||||||
|
.generate_commit_from_repo(repo, format, language)
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
generator.generate_commit_interactive(repo, format).await?
|
generator
|
||||||
|
.generate_commit_interactive(repo, format, language)
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(generated.to_conventional())
|
Ok(generated.to_conventional())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_interactive_commit(&self, format: CommitFormat) -> Result<String> {
|
async fn create_interactive_commit(
|
||||||
|
&self,
|
||||||
|
format: CommitFormat,
|
||||||
|
messages: &Messages,
|
||||||
|
) -> Result<String> {
|
||||||
let types = get_commit_types(format == CommitFormat::Commitlint);
|
let types = get_commit_types(format == CommitFormat::Commitlint);
|
||||||
|
|
||||||
// Select type
|
// Select type
|
||||||
let type_idx = Select::new()
|
let type_idx = Select::new()
|
||||||
.with_prompt("Select commit type")
|
.with_prompt(messages.select_commit_type())
|
||||||
.items(types)
|
.items(types)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
let commit_type = types[type_idx].to_string();
|
let commit_type = types[type_idx].to_string();
|
||||||
|
|
||||||
// Enter scope (optional)
|
// Enter scope (optional)
|
||||||
let scope: String = Input::new()
|
let scope: String = Input::new()
|
||||||
.with_prompt("Scope (optional, press Enter to skip)")
|
.with_prompt(messages.scope_optional())
|
||||||
.allow_empty(true)
|
.allow_empty(true)
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
let scope = if scope.is_empty() { None } else { Some(scope) };
|
let scope = if scope.is_empty() { None } else { Some(scope) };
|
||||||
|
|
||||||
// Enter description
|
// Enter description
|
||||||
let description: String = Input::new()
|
let description: String = Input::new()
|
||||||
.with_prompt("Description")
|
.with_prompt(messages.description())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
// Breaking change
|
// Breaking change
|
||||||
let breaking = Confirm::new()
|
let breaking = Confirm::new()
|
||||||
.with_prompt("Is this a breaking change?")
|
.with_prompt(messages.breaking_change())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
// Add body
|
// Add body
|
||||||
let add_body = Confirm::new()
|
let add_body = Confirm::new()
|
||||||
.with_prompt("Add body to commit?")
|
.with_prompt(messages.add_body())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let body = if add_body {
|
let body = if add_body {
|
||||||
let body_text = crate::utils::editor::edit_content("Enter commit body...")?;
|
let body_text = crate::utils::editor::edit_content(messages.enter_commit_body())?;
|
||||||
if body_text.trim().is_empty() {
|
if body_text.trim().is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@@ -288,34 +381,50 @@ impl CommitCommand {
|
|||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
bail!("Failed to amend commit: {}", stderr);
|
|
||||||
|
let error_msg = if stderr.is_empty() {
|
||||||
|
if stdout.is_empty() {
|
||||||
|
"GPG signing failed. Please check:\n\
|
||||||
|
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||||
|
2. GPG agent is running\n\
|
||||||
|
3. You can sign commits manually (try: git commit --amend -S)"
|
||||||
|
.to_string()
|
||||||
|
} else {
|
||||||
|
stdout.to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stderr.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
bail!("Failed to amend commit: {}", error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper trait for optional builder methods
|
// // Helper trait for optional builder methods
|
||||||
trait CommitBuilderExt {
|
// trait CommitBuilderExt {
|
||||||
fn scope_opt(self, scope: Option<String>) -> Self;
|
// fn scope_opt(self, scope: Option<String>) -> Self;
|
||||||
fn body_opt(self, body: Option<String>) -> Self;
|
// fn body_opt(self, body: Option<String>) -> Self;
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl CommitBuilderExt for CommitBuilder {
|
// impl CommitBuilderExt for CommitBuilder {
|
||||||
fn scope_opt(self, scope: Option<String>) -> Self {
|
// fn scope_opt(self, scope: Option<String>) -> Self {
|
||||||
if let Some(s) = scope {
|
// if let Some(s) = scope {
|
||||||
self.scope(s)
|
// self.scope(s)
|
||||||
} else {
|
// } else {
|
||||||
self
|
// self
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn body_opt(self, body: Option<String>) -> Self {
|
// fn body_opt(self, body: Option<String>) -> Self {
|
||||||
if let Some(b) = body {
|
// if let Some(b) = body {
|
||||||
self.body(b)
|
// self.body(b)
|
||||||
} else {
|
// } else {
|
||||||
self
|
// self
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -2,10 +2,13 @@ use anyhow::Result;
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use dialoguer::{Confirm, Input, Select};
|
use dialoguer::{Confirm, Input, Select};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::config::{GitProfile};
|
|
||||||
use crate::config::manager::ConfigManager;
|
use crate::config::manager::ConfigManager;
|
||||||
use crate::config::profile::{GpgConfig, SshConfig};
|
use crate::config::profile::{GpgConfig, SshConfig};
|
||||||
|
use crate::config::{GitProfile, Language};
|
||||||
|
use crate::i18n::Messages;
|
||||||
|
use crate::utils::keyring::{get_default_model, get_supported_providers, provider_needs_api_key};
|
||||||
use crate::utils::validators::validate_email;
|
use crate::utils::validators::validate_email;
|
||||||
|
|
||||||
/// Initialize quicommit configuration
|
/// Initialize quicommit configuration
|
||||||
@@ -21,12 +24,13 @@ pub struct InitCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InitCommand {
|
impl InitCommand {
|
||||||
pub async fn execute(&self) -> Result<()> {
|
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||||
println!("{}", "🚀 Initializing QuiCommit...".bold().cyan());
|
let messages = Messages::new(Language::English);
|
||||||
|
println!("{}", messages.initializing().bold().cyan());
|
||||||
|
|
||||||
let config_path = crate::config::AppConfig::default_path()?;
|
let config_path =
|
||||||
|
config_path.unwrap_or_else(|| crate::config::AppConfig::default_path().unwrap());
|
||||||
|
|
||||||
// Check if config already exists
|
|
||||||
if config_path.exists() && !self.reset {
|
if config_path.exists() && !self.reset {
|
||||||
if !self.yes {
|
if !self.yes {
|
||||||
let overwrite = Confirm::new()
|
let overwrite = Confirm::new()
|
||||||
@@ -38,28 +42,36 @@ impl InitCommand {
|
|||||||
println!("{}", "Initialization cancelled.".yellow());
|
println!("{}", "Initialization cancelled.".yellow());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
"Configuration already exists. Use --reset to overwrite.".yellow()
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut manager = if self.reset {
|
if let Some(parent) = config_path.parent() {
|
||||||
ConfigManager::new()?
|
std::fs::create_dir_all(parent)
|
||||||
} else {
|
.map_err(|e| anyhow::anyhow!("Failed to create config directory: {}", e))?;
|
||||||
ConfigManager::new().or_else(|_| Ok::<_, anyhow::Error>(ConfigManager::default()))?
|
}
|
||||||
};
|
|
||||||
|
let mut manager = ConfigManager::with_path_fresh(&config_path)?;
|
||||||
|
|
||||||
if self.yes {
|
if self.yes {
|
||||||
// Quick setup with defaults
|
|
||||||
self.quick_setup(&mut manager).await?;
|
self.quick_setup(&mut manager).await?;
|
||||||
} else {
|
} else {
|
||||||
// Interactive setup
|
|
||||||
self.interactive_setup(&mut manager).await?;
|
self.interactive_setup(&mut manager).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
manager.save()?;
|
manager.save()?;
|
||||||
|
|
||||||
println!("{}", "✅ QuiCommit initialized successfully!".bold().green());
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
println!("\nConfig file: {}", config_path.display());
|
let messages = Messages::new(language);
|
||||||
println!("\nNext steps:");
|
|
||||||
|
println!("{}", messages.init_success().bold().green());
|
||||||
|
println!("\n{}: {}", messages.config_file(), config_path.display());
|
||||||
|
println!("\n{}:", messages.next_steps());
|
||||||
println!(" 1. Create a profile: {}", "quicommit profile add".cyan());
|
println!(" 1. Create a profile: {}", "quicommit profile add".cyan());
|
||||||
println!(" 2. Configure LLM: {}", "quicommit config set-llm".cyan());
|
println!(" 2. Configure LLM: {}", "quicommit config set-llm".cyan());
|
||||||
println!(" 3. Start committing: {}", "quicommit commit".cyan());
|
println!(" 3. Start committing: {}", "quicommit commit".cyan());
|
||||||
@@ -68,108 +80,121 @@ impl InitCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn quick_setup(&self, manager: &mut ConfigManager) -> Result<()> {
|
async fn quick_setup(&self, manager: &mut ConfigManager) -> Result<()> {
|
||||||
// Try to get git user info
|
|
||||||
let git_config = git2::Config::open_default()?;
|
let git_config = git2::Config::open_default()?;
|
||||||
|
|
||||||
let user_name = git_config.get_string("user.name").unwrap_or_else(|_| "User".to_string());
|
let user_name = git_config
|
||||||
let user_email = git_config.get_string("user.email").unwrap_or_else(|_| "user@example.com".to_string());
|
.get_string("user.name")
|
||||||
|
.unwrap_or_else(|_| "User".to_string());
|
||||||
|
let user_email = git_config
|
||||||
|
.get_string("user.email")
|
||||||
|
.unwrap_or_else(|_| "user@example.com".to_string());
|
||||||
|
|
||||||
let profile = GitProfile::new(
|
let profile = GitProfile::new("default".to_string(), user_name, user_email);
|
||||||
"default".to_string(),
|
|
||||||
user_name,
|
|
||||||
user_email,
|
|
||||||
);
|
|
||||||
|
|
||||||
manager.add_profile("default".to_string(), profile)?;
|
manager.add_profile("default".to_string(), profile)?;
|
||||||
manager.set_default_profile(Some("default".to_string()))?;
|
manager.set_default_profile(Some("default".to_string()))?;
|
||||||
|
|
||||||
// Set default LLM to Ollama
|
|
||||||
manager.set_llm_provider("ollama".to_string());
|
manager.set_llm_provider("ollama".to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn interactive_setup(&self, manager: &mut ConfigManager) -> Result<()> {
|
async fn interactive_setup(&self, manager: &mut ConfigManager) -> Result<()> {
|
||||||
println!("\n{}", "Let's set up your first profile:".bold());
|
let messages = Messages::new(Language::English);
|
||||||
|
println!("\n{}", messages.setup_profile().bold());
|
||||||
|
|
||||||
|
println!("\n{}", messages.select_output_language().bold());
|
||||||
|
let languages = [
|
||||||
|
Language::English,
|
||||||
|
Language::Chinese,
|
||||||
|
Language::Japanese,
|
||||||
|
Language::Korean,
|
||||||
|
Language::Spanish,
|
||||||
|
Language::French,
|
||||||
|
Language::German,
|
||||||
|
];
|
||||||
|
let language_names: Vec<String> = languages
|
||||||
|
.iter()
|
||||||
|
.map(|l| l.display_name().to_string())
|
||||||
|
.collect();
|
||||||
|
let language_idx = Select::new().items(&language_names).default(0).interact()?;
|
||||||
|
|
||||||
|
let selected_language = languages[language_idx];
|
||||||
|
manager.set_output_language(selected_language.to_code().to_string());
|
||||||
|
|
||||||
|
let messages = Messages::new(selected_language);
|
||||||
|
|
||||||
// Profile name
|
|
||||||
let profile_name: String = Input::new()
|
let profile_name: String = Input::new()
|
||||||
.with_prompt("Profile name")
|
.with_prompt(messages.profile_name())
|
||||||
.default("personal".to_string())
|
.default("personal".to_string())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
// User info
|
|
||||||
let git_config = git2::Config::open_default().ok();
|
let git_config = git2::Config::open_default().ok();
|
||||||
|
|
||||||
let default_name = git_config.as_ref()
|
let default_name = git_config
|
||||||
|
.as_ref()
|
||||||
.and_then(|c| c.get_string("user.name").ok())
|
.and_then(|c| c.get_string("user.name").ok())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let default_email = git_config.as_ref()
|
let default_email = git_config
|
||||||
|
.as_ref()
|
||||||
.and_then(|c| c.get_string("user.email").ok())
|
.and_then(|c| c.get_string("user.email").ok())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let user_name: String = Input::new()
|
let user_name: String = Input::new()
|
||||||
.with_prompt("Git user name")
|
.with_prompt(messages.git_user_name())
|
||||||
.default(default_name)
|
.default(default_name)
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
let user_email: String = Input::new()
|
let user_email: String = Input::new()
|
||||||
.with_prompt("Git user email")
|
.with_prompt(messages.git_user_email())
|
||||||
.default(default_email)
|
.default(default_email)
|
||||||
.validate_with(|input: &String| {
|
.validate_with(|input: &String| validate_email(input).map_err(|e| e.to_string()))
|
||||||
validate_email(input).map_err(|e| e.to_string())
|
|
||||||
})
|
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
let description: String = Input::new()
|
let description: String = Input::new()
|
||||||
.with_prompt("Profile description (optional)")
|
.with_prompt(messages.profile_description())
|
||||||
.allow_empty(true)
|
.allow_empty(true)
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
let is_work = Confirm::new()
|
let is_work = Confirm::new()
|
||||||
.with_prompt("Is this a work profile?")
|
.with_prompt(messages.is_work_profile())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let organization = if is_work {
|
let organization = if is_work {
|
||||||
Some(Input::new()
|
Some(
|
||||||
.with_prompt("Organization/Company name")
|
Input::new()
|
||||||
.interact_text()?)
|
.with_prompt(messages.organization_name())
|
||||||
|
.interact_text()?,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// SSH configuration
|
|
||||||
let setup_ssh = Confirm::new()
|
let setup_ssh = Confirm::new()
|
||||||
.with_prompt("Configure SSH key?")
|
.with_prompt(messages.configure_ssh())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let ssh_config = if setup_ssh {
|
let ssh_config = if setup_ssh {
|
||||||
Some(self.setup_ssh_interactive().await?)
|
Some(self.setup_ssh_interactive(&messages).await?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// GPG configuration
|
|
||||||
let setup_gpg = Confirm::new()
|
let setup_gpg = Confirm::new()
|
||||||
.with_prompt("Configure GPG signing?")
|
.with_prompt(messages.configure_gpg())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let gpg_config = if setup_gpg {
|
let gpg_config = if setup_gpg {
|
||||||
Some(self.setup_gpg_interactive().await?)
|
Some(self.setup_gpg_interactive(&messages).await?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create profile
|
let mut profile = GitProfile::new(profile_name.clone(), user_name, user_email);
|
||||||
let mut profile = GitProfile::new(
|
|
||||||
profile_name.clone(),
|
|
||||||
user_name,
|
|
||||||
user_email,
|
|
||||||
);
|
|
||||||
|
|
||||||
if !description.is_empty() {
|
if !description.is_empty() {
|
||||||
profile.description = Some(description);
|
profile.description = Some(description);
|
||||||
@@ -183,40 +208,118 @@ impl InitCommand {
|
|||||||
manager.add_profile(profile_name.clone(), profile)?;
|
manager.add_profile(profile_name.clone(), profile)?;
|
||||||
manager.set_default_profile(Some(profile_name))?;
|
manager.set_default_profile(Some(profile_name))?;
|
||||||
|
|
||||||
// LLM provider selection
|
println!("\n{}", messages.select_llm_provider().bold());
|
||||||
println!("\n{}", "Select your preferred LLM provider:".bold());
|
|
||||||
let providers = vec!["Ollama (local)", "OpenAI", "Anthropic Claude"];
|
let provider_display_names = vec![
|
||||||
|
"Ollama (local)",
|
||||||
|
"OpenAI",
|
||||||
|
"Anthropic Claude",
|
||||||
|
"Kimi (Moonshot AI)",
|
||||||
|
"DeepSeek",
|
||||||
|
"OpenRouter",
|
||||||
|
];
|
||||||
|
|
||||||
let provider_idx = Select::new()
|
let provider_idx = Select::new()
|
||||||
.items(&providers)
|
.items(&provider_display_names)
|
||||||
.default(0)
|
.default(0)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let provider = match provider_idx {
|
let providers = get_supported_providers();
|
||||||
0 => "ollama",
|
let provider = providers[provider_idx].to_string();
|
||||||
1 => "openai",
|
|
||||||
2 => "anthropic",
|
let keyring = manager.keyring();
|
||||||
_ => "ollama",
|
let keyring_available = keyring.is_available();
|
||||||
|
|
||||||
|
if !keyring_available {
|
||||||
|
println!(
|
||||||
|
"\n{}",
|
||||||
|
"⚠ Keyring is not available on this system.".yellow()
|
||||||
|
);
|
||||||
|
println!("{}", keyring.get_status_message().yellow());
|
||||||
|
}
|
||||||
|
|
||||||
|
let api_key = if provider_needs_api_key(&provider) {
|
||||||
|
let env_key = std::env::var("QUICOMMIT_API_KEY")
|
||||||
|
.or_else(|_| {
|
||||||
|
std::env::var(format!("QUICOMMIT_{}_API_KEY", provider.to_uppercase()))
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
if let Some(_key) = env_key {
|
||||||
|
println!(
|
||||||
|
"\n{} {}",
|
||||||
|
"✓".green(),
|
||||||
|
"Found API key in environment variable.".green()
|
||||||
|
);
|
||||||
|
None
|
||||||
|
} else if keyring_available {
|
||||||
|
let prompt = match provider.as_str() {
|
||||||
|
"openai" => messages.openai_api_key(),
|
||||||
|
"anthropic" => messages.anthropic_api_key(),
|
||||||
|
"kimi" => messages.kimi_api_key(),
|
||||||
|
"deepseek" => messages.deepseek_api_key(),
|
||||||
|
"openrouter" => messages.openrouter_api_key(),
|
||||||
|
_ => "API Key",
|
||||||
};
|
};
|
||||||
|
|
||||||
manager.set_llm_provider(provider.to_string());
|
let key: String = Input::new().with_prompt(prompt).interact_text()?;
|
||||||
|
Some(key)
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"\n{}",
|
||||||
|
"Please set the QUICOMMIT_API_KEY environment variable.".yellow()
|
||||||
|
);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
// Configure API key if needed
|
let default_model = get_default_model(&provider);
|
||||||
if provider == "openai" {
|
let model: String = Input::new()
|
||||||
let api_key: String = Input::new()
|
.with_prompt("Model name")
|
||||||
.with_prompt("OpenAI API key")
|
.default(default_model.to_string())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
manager.set_openai_api_key(api_key);
|
|
||||||
} else if provider == "anthropic" {
|
let base_url: Option<String> = if provider == "ollama" {
|
||||||
let api_key: String = Input::new()
|
let url: String = Input::new()
|
||||||
.with_prompt("Anthropic API key")
|
.with_prompt("Ollama server URL")
|
||||||
|
.default("http://localhost:11434".to_string())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
manager.set_anthropic_api_key(api_key);
|
Some(url)
|
||||||
|
} else {
|
||||||
|
let use_custom_url = Confirm::new()
|
||||||
|
.with_prompt("Use custom API base URL?")
|
||||||
|
.default(false)
|
||||||
|
.interact()?;
|
||||||
|
|
||||||
|
if use_custom_url {
|
||||||
|
let url: String = Input::new().with_prompt("Base URL").interact_text()?;
|
||||||
|
Some(url)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
manager.set_llm_provider(provider.clone());
|
||||||
|
manager.set_llm_model(model);
|
||||||
|
manager.set_llm_base_url(base_url);
|
||||||
|
|
||||||
|
if let Some(key) = api_key
|
||||||
|
&& provider_needs_api_key(&provider)
|
||||||
|
{
|
||||||
|
manager.set_api_key(&key)?;
|
||||||
|
println!(
|
||||||
|
"\n{} {}",
|
||||||
|
"✓".green(),
|
||||||
|
"API key stored securely in system keyring.".green()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup_ssh_interactive(&self) -> Result<SshConfig> {
|
async fn setup_ssh_interactive(&self, messages: &Messages) -> Result<SshConfig> {
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
let ssh_dir = dirs::home_dir()
|
let ssh_dir = dirs::home_dir()
|
||||||
@@ -224,17 +327,17 @@ impl InitCommand {
|
|||||||
.unwrap_or_else(|| PathBuf::from("~/.ssh"));
|
.unwrap_or_else(|| PathBuf::from("~/.ssh"));
|
||||||
|
|
||||||
let key_path: String = Input::new()
|
let key_path: String = Input::new()
|
||||||
.with_prompt("SSH private key path")
|
.with_prompt(messages.ssh_private_key_path())
|
||||||
.default(ssh_dir.join("id_rsa").display().to_string())
|
.default(ssh_dir.join("id_rsa").display().to_string())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
let has_passphrase = Confirm::new()
|
let has_passphrase = Confirm::new()
|
||||||
.with_prompt("Does this key have a passphrase?")
|
.with_prompt(messages.has_passphrase())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let passphrase = if has_passphrase {
|
let passphrase = if has_passphrase {
|
||||||
Some(crate::utils::password_input("SSH key passphrase")?)
|
Some(crate::utils::password_input(messages.ssh_key_passphrase())?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -249,13 +352,13 @@ impl InitCommand {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup_gpg_interactive(&self) -> Result<GpgConfig> {
|
async fn setup_gpg_interactive(&self, messages: &Messages) -> Result<GpgConfig> {
|
||||||
let key_id: String = Input::new()
|
let key_id: String = Input::new()
|
||||||
.with_prompt("GPG key ID")
|
.with_prompt(messages.gpg_key_id())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
|
|
||||||
let use_agent = Confirm::new()
|
let use_agent = Confirm::new()
|
||||||
.with_prompt("Use GPG agent?")
|
.with_prompt(messages.use_gpg_agent())
|
||||||
.default(true)
|
.default(true)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,15 +1,17 @@
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{Result, bail};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use dialoguer::{Confirm, Input, Select};
|
use dialoguer::{Confirm, Input, Select};
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::config::manager::ConfigManager;
|
use crate::config::{Language, manager::ConfigManager};
|
||||||
use crate::git::{find_repo, GitRepo};
|
|
||||||
use crate::generator::ContentGenerator;
|
use crate::generator::ContentGenerator;
|
||||||
use crate::git::tag::{
|
use crate::git::tag::{
|
||||||
bump_version, get_latest_version, suggest_version_bump, TagBuilder, VersionBump,
|
TagBuilder, VersionBump, bump_version, get_latest_version, suggest_version_bump,
|
||||||
};
|
};
|
||||||
|
use crate::git::{GitRepo, find_repo};
|
||||||
|
use crate::i18n::Messages;
|
||||||
|
|
||||||
/// Generate and create Git tags
|
/// Generate and create Git tags
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -54,16 +56,26 @@ pub struct TagCommand {
|
|||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
|
|
||||||
|
/// Enable thinking mode for this tag (override config)
|
||||||
|
#[arg(short = 't', long)]
|
||||||
|
think: bool,
|
||||||
|
|
||||||
/// Skip interactive prompts
|
/// Skip interactive prompts
|
||||||
#[arg(short = 'y', long)]
|
#[arg(short = 'y', long)]
|
||||||
yes: bool,
|
yes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TagCommand {
|
impl TagCommand {
|
||||||
pub async fn execute(&self) -> Result<()> {
|
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||||
let manager = ConfigManager::new()?;
|
let manager = if let Some(ref path) = config_path {
|
||||||
|
ConfigManager::with_path(path)?
|
||||||
|
} else {
|
||||||
|
ConfigManager::new()?
|
||||||
|
};
|
||||||
let config = manager.config();
|
let config = manager.config();
|
||||||
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
let messages = Messages::new(language);
|
||||||
|
|
||||||
// Determine tag name
|
// Determine tag name
|
||||||
let tag_name = if let Some(name) = &self.name {
|
let tag_name = if let Some(name) = &self.name {
|
||||||
@@ -71,8 +83,8 @@ impl TagCommand {
|
|||||||
} else if let Some(bump_str) = &self.bump {
|
} else if let Some(bump_str) = &self.bump {
|
||||||
// Calculate bumped version
|
// Calculate bumped version
|
||||||
let prefix = &config.tag.version_prefix;
|
let prefix = &config.tag.version_prefix;
|
||||||
let latest = get_latest_version(&repo, prefix)?
|
let latest =
|
||||||
.unwrap_or_else(|| Version::new(0, 0, 0));
|
get_latest_version(&repo, prefix)?.unwrap_or_else(|| Version::new(0, 0, 0));
|
||||||
|
|
||||||
let bump = VersionBump::from_str(bump_str)?;
|
let bump = VersionBump::from_str(bump_str)?;
|
||||||
let new_version = bump_version(&latest, bump, None);
|
let new_version = bump_version(&latest, bump, None);
|
||||||
@@ -80,11 +92,18 @@ impl TagCommand {
|
|||||||
format!("{}{}", prefix, new_version)
|
format!("{}{}", prefix, new_version)
|
||||||
} else {
|
} else {
|
||||||
// Interactive mode
|
// Interactive mode
|
||||||
self.select_version_interactive(&repo, &config.tag.version_prefix).await?
|
self.select_version_interactive(&repo, &config.tag.version_prefix, &messages)
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate tag name (if it looks like a version)
|
// Validate tag name (if it looks like a version)
|
||||||
if tag_name.starts_with('v') || tag_name.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false) {
|
if tag_name.starts_with('v')
|
||||||
|
|| tag_name
|
||||||
|
.chars()
|
||||||
|
.next()
|
||||||
|
.map(|c| c.is_ascii_digit())
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
let version_str = tag_name.trim_start_matches('v');
|
let version_str = tag_name.trim_start_matches('v');
|
||||||
if let Err(e) = crate::utils::validators::validate_semver(version_str) {
|
if let Err(e) = crate::utils::validators::validate_semver(version_str) {
|
||||||
println!("{}: {}", "Warning".yellow(), e);
|
println!("{}: {}", "Warning".yellow(), e);
|
||||||
@@ -96,7 +115,7 @@ impl TagCommand {
|
|||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
if !proceed {
|
if !proceed {
|
||||||
bail!("Tag creation cancelled");
|
bail!("{}", messages.tag_cancelled());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,16 +127,19 @@ impl TagCommand {
|
|||||||
} else if let Some(msg) = &self.message {
|
} else if let Some(msg) = &self.message {
|
||||||
Some(msg.clone())
|
Some(msg.clone())
|
||||||
} else if self.generate || (config.tag.auto_generate && !self.yes) {
|
} else if self.generate || (config.tag.auto_generate && !self.yes) {
|
||||||
Some(self.generate_tag_message(&repo, &tag_name).await?)
|
Some(
|
||||||
|
self.generate_tag_message(&repo, &tag_name, &messages)
|
||||||
|
.await?,
|
||||||
|
)
|
||||||
} else if !self.yes {
|
} else if !self.yes {
|
||||||
Some(self.input_message_interactive(&tag_name)?)
|
Some(self.input_message_interactive(&tag_name, &messages)?)
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Release {}", tag_name))
|
Some(format!("Release {}", tag_name))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Show preview
|
// Show preview
|
||||||
println!("\n{}", "─".repeat(60));
|
println!("\n{}", "─".repeat(60));
|
||||||
println!("{}", "Tag preview:".bold());
|
println!("{}", messages.tag_preview().bold());
|
||||||
println!("{}", "─".repeat(60));
|
println!("{}", "─".repeat(60));
|
||||||
println!("Name: {}", tag_name.cyan());
|
println!("Name: {}", tag_name.cyan());
|
||||||
if let Some(ref msg) = message {
|
if let Some(ref msg) = message {
|
||||||
@@ -129,22 +151,21 @@ impl TagCommand {
|
|||||||
|
|
||||||
if !self.yes {
|
if !self.yes {
|
||||||
let confirm = Confirm::new()
|
let confirm = Confirm::new()
|
||||||
.with_prompt("Create this tag?")
|
.with_prompt(messages.create_tag())
|
||||||
.default(true)
|
.default(true)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
if !confirm {
|
if !confirm {
|
||||||
println!("{}", "Tag creation cancelled.".yellow());
|
println!("{}", messages.tag_cancelled().yellow());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.dry_run {
|
if self.dry_run {
|
||||||
println!("\n{}", "Dry run - tag not created.".yellow());
|
println!("\n{} {}", messages.dry_run(), "- tag not created.".yellow());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create tag
|
|
||||||
let builder = TagBuilder::new()
|
let builder = TagBuilder::new()
|
||||||
.name(&tag_name)
|
.name(&tag_name)
|
||||||
.message_opt(message)
|
.message_opt(message)
|
||||||
@@ -154,41 +175,57 @@ impl TagCommand {
|
|||||||
|
|
||||||
builder.execute(&repo)?;
|
builder.execute(&repo)?;
|
||||||
|
|
||||||
println!("{} Created tag {}", "✓".green(), tag_name.cyan());
|
println!("{} {}", messages.tag_created().green(), tag_name.cyan());
|
||||||
|
|
||||||
// Push if requested
|
// Push if requested or ask user
|
||||||
if self.push {
|
if self.push {
|
||||||
println!("{} Pushing tag to {}...", "→".blue(), &self.remote);
|
println!("{}", messages.pushing_tag(&self.remote));
|
||||||
repo.push(&self.remote, &format!("refs/tags/{}", tag_name))?;
|
repo.push(&self.remote, &format!("refs/tags/{}", tag_name))?;
|
||||||
println!("{} Pushed tag to {}", "✓".green(), &self.remote);
|
println!("{}", messages.pushed_tag(&self.remote));
|
||||||
|
} else if !self.yes && !self.dry_run {
|
||||||
|
let should_push = Confirm::new()
|
||||||
|
.with_prompt(messages.push_after_tag())
|
||||||
|
.default(false)
|
||||||
|
.interact()?;
|
||||||
|
|
||||||
|
if should_push {
|
||||||
|
println!("{}", messages.pushing_tag(&self.remote));
|
||||||
|
repo.push(&self.remote, &format!("refs/tags/{}", tag_name))?;
|
||||||
|
println!("{}", messages.pushed_tag(&self.remote));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn select_version_interactive(&self, repo: &GitRepo, prefix: &str) -> Result<String> {
|
async fn select_version_interactive(
|
||||||
|
&self,
|
||||||
|
repo: &GitRepo,
|
||||||
|
prefix: &str,
|
||||||
|
messages: &Messages,
|
||||||
|
) -> Result<String> {
|
||||||
loop {
|
loop {
|
||||||
let latest = get_latest_version(repo, prefix)?;
|
let latest = get_latest_version(repo, prefix)?;
|
||||||
|
|
||||||
println!("\n{}", "Version selection:".bold());
|
println!("\n{}", messages.version_selection().bold());
|
||||||
|
|
||||||
if let Some(ref version) = latest {
|
if let Some(ref version) = latest {
|
||||||
println!("Latest version: {}{}", prefix, version);
|
println!("{} {}{}", messages.latest_version(), prefix, version);
|
||||||
} else {
|
} else {
|
||||||
println!("No existing version tags found");
|
println!("{}", messages.no_existing_version_tags());
|
||||||
}
|
}
|
||||||
|
|
||||||
let options = vec![
|
let options = vec![
|
||||||
"Auto-detect bump from commits",
|
messages.auto_detect_bump(),
|
||||||
"Bump major version",
|
messages.bump_major_version(),
|
||||||
"Bump minor version",
|
messages.bump_minor_version(),
|
||||||
"Bump patch version",
|
messages.bump_patch_version(),
|
||||||
"Enter custom version",
|
messages.enter_custom_version(),
|
||||||
"Enter custom tag name",
|
messages.enter_custom_tag_name(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let selection = Select::new()
|
let selection = Select::new()
|
||||||
.with_prompt("Select option")
|
.with_prompt(messages.select_option())
|
||||||
.items(&options)
|
.items(&options)
|
||||||
.default(0)
|
.default(0)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
@@ -198,14 +235,21 @@ impl TagCommand {
|
|||||||
// Auto-detect
|
// Auto-detect
|
||||||
let commits = repo.get_commits(50)?;
|
let commits = repo.get_commits(50)?;
|
||||||
let bump = suggest_version_bump(&commits);
|
let bump = suggest_version_bump(&commits);
|
||||||
let version = latest.as_ref()
|
let version = latest
|
||||||
|
.as_ref()
|
||||||
.map(|v| bump_version(v, bump, None))
|
.map(|v| bump_version(v, bump, None))
|
||||||
.unwrap_or_else(|| Version::new(0, 1, 0));
|
.unwrap_or_else(|| Version::new(0, 1, 0));
|
||||||
|
|
||||||
println!("Suggested bump: {:?} → {}{}", bump, prefix, version);
|
println!(
|
||||||
|
"{} {:?} → {}{}",
|
||||||
|
messages.suggested_bump(),
|
||||||
|
bump,
|
||||||
|
prefix,
|
||||||
|
version
|
||||||
|
);
|
||||||
|
|
||||||
let confirm = Confirm::new()
|
let confirm = Confirm::new()
|
||||||
.with_prompt("Use this version?")
|
.with_prompt(messages.use_this_version())
|
||||||
.default(true)
|
.default(true)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
@@ -215,33 +259,36 @@ impl TagCommand {
|
|||||||
// User rejected, continue the loop
|
// User rejected, continue the loop
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
let version = latest.as_ref()
|
let version = latest
|
||||||
|
.as_ref()
|
||||||
.map(|v| bump_version(v, VersionBump::Major, None))
|
.map(|v| bump_version(v, VersionBump::Major, None))
|
||||||
.unwrap_or_else(|| Version::new(1, 0, 0));
|
.unwrap_or_else(|| Version::new(1, 0, 0));
|
||||||
return Ok(format!("{}{}", prefix, version));
|
return Ok(format!("{}{}", prefix, version));
|
||||||
}
|
}
|
||||||
2 => {
|
2 => {
|
||||||
let version = latest.as_ref()
|
let version = latest
|
||||||
|
.as_ref()
|
||||||
.map(|v| bump_version(v, VersionBump::Minor, None))
|
.map(|v| bump_version(v, VersionBump::Minor, None))
|
||||||
.unwrap_or_else(|| Version::new(0, 1, 0));
|
.unwrap_or_else(|| Version::new(0, 1, 0));
|
||||||
return Ok(format!("{}{}", prefix, version));
|
return Ok(format!("{}{}", prefix, version));
|
||||||
}
|
}
|
||||||
3 => {
|
3 => {
|
||||||
let version = latest.as_ref()
|
let version = latest
|
||||||
|
.as_ref()
|
||||||
.map(|v| bump_version(v, VersionBump::Patch, None))
|
.map(|v| bump_version(v, VersionBump::Patch, None))
|
||||||
.unwrap_or_else(|| Version::new(0, 0, 1));
|
.unwrap_or_else(|| Version::new(0, 0, 1));
|
||||||
return Ok(format!("{}{}", prefix, version));
|
return Ok(format!("{}{}", prefix, version));
|
||||||
}
|
}
|
||||||
4 => {
|
4 => {
|
||||||
let input: String = Input::new()
|
let input: String = Input::new()
|
||||||
.with_prompt("Enter version (e.g., 1.2.3)")
|
.with_prompt(messages.enter_version())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
let version = Version::parse(&input)?;
|
let version = Version::parse(&input)?;
|
||||||
return Ok(format!("{}{}", prefix, version));
|
return Ok(format!("{}{}", prefix, version));
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
let input: String = Input::new()
|
let input: String = Input::new()
|
||||||
.with_prompt("Enter tag name")
|
.with_prompt(messages.enter_tag_name())
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
return Ok(input);
|
return Ok(input);
|
||||||
}
|
}
|
||||||
@@ -250,11 +297,15 @@ impl TagCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_tag_message(&self, repo: &GitRepo, version: &str) -> Result<String> {
|
async fn generate_tag_message(
|
||||||
|
&self,
|
||||||
|
repo: &GitRepo,
|
||||||
|
version: &str,
|
||||||
|
messages: &Messages,
|
||||||
|
) -> Result<String> {
|
||||||
let manager = ConfigManager::new()?;
|
let manager = ConfigManager::new()?;
|
||||||
let config = manager.config();
|
let language = manager.get_language().unwrap_or(Language::English);
|
||||||
|
|
||||||
// Get commits since last tag
|
|
||||||
let tags = repo.get_tags()?;
|
let tags = repo.get_tags()?;
|
||||||
let commits = if let Some(latest_tag) = tags.first() {
|
let commits = if let Some(latest_tag) = tags.first() {
|
||||||
repo.get_commits_between(&latest_tag.name, "HEAD")?
|
repo.get_commits_between(&latest_tag.name, "HEAD")?
|
||||||
@@ -266,17 +317,19 @@ impl TagCommand {
|
|||||||
return Ok(format!("Release {}", version));
|
return Ok(format!("Release {}", version));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{} AI is generating tag message from {} commits...", "🤖", commits.len());
|
println!("{}", messages.ai_generating_tag(commits.len()));
|
||||||
|
|
||||||
let generator = ContentGenerator::new(&config.llm).await?;
|
let generator = ContentGenerator::new_with_think(&manager, self.think).await?;
|
||||||
generator.generate_tag_message(version, &commits).await
|
generator
|
||||||
|
.generate_tag_message(version, &commits, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_message_interactive(&self, version: &str) -> Result<String> {
|
fn input_message_interactive(&self, version: &str, messages: &Messages) -> Result<String> {
|
||||||
let default_msg = format!("Release {}", version);
|
let default_msg = format!("Release {}", version);
|
||||||
|
|
||||||
let use_editor = Confirm::new()
|
let use_editor = Confirm::new()
|
||||||
.with_prompt("Open editor for tag message?")
|
.with_prompt(messages.open_editor())
|
||||||
.default(false)
|
.default(false)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
@@ -284,7 +337,7 @@ impl TagCommand {
|
|||||||
crate::utils::editor::edit_content(&default_msg)
|
crate::utils::editor::edit_content(&default_msg)
|
||||||
} else {
|
} else {
|
||||||
Ok(Input::new()
|
Ok(Input::new()
|
||||||
.with_prompt("Tag message")
|
.with_prompt(messages.tag_message())
|
||||||
.default(default_msg)
|
.default(default_msg)
|
||||||
.interact_text()?)
|
.interact_text()?)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
use super::{AppConfig, GitProfile, TokenConfig, TokenType};
|
use super::{AppConfig, GitProfile, TokenConfig};
|
||||||
use anyhow::{bail, Context, Result};
|
use crate::utils::keyring::{
|
||||||
use std::collections::HashMap;
|
KeyringManager, get_default_base_url, get_default_model, provider_needs_api_key,
|
||||||
|
};
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
|
// use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
/// Configuration manager
|
/// Configuration manager
|
||||||
@@ -8,6 +11,7 @@ pub struct ConfigManager {
|
|||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
config_path: PathBuf,
|
config_path: PathBuf,
|
||||||
modified: bool,
|
modified: bool,
|
||||||
|
keyring: KeyringManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigManager {
|
impl ConfigManager {
|
||||||
@@ -19,11 +23,26 @@ impl ConfigManager {
|
|||||||
|
|
||||||
/// Create config manager with specific path
|
/// Create config manager with specific path
|
||||||
pub fn with_path(path: &Path) -> Result<Self> {
|
pub fn with_path(path: &Path) -> Result<Self> {
|
||||||
let config = AppConfig::load(path)?;
|
let config = if path.exists() {
|
||||||
|
AppConfig::load(path)?
|
||||||
|
} else {
|
||||||
|
AppConfig::default()
|
||||||
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
config,
|
config,
|
||||||
config_path: path.to_path_buf(),
|
config_path: path.to_path_buf(),
|
||||||
modified: false,
|
modified: false,
|
||||||
|
keyring: KeyringManager::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create config manager with fresh config (ignoring existing)
|
||||||
|
pub fn with_path_fresh(path: &Path) -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
config: AppConfig::default(),
|
||||||
|
config_path: path.to_path_buf(),
|
||||||
|
modified: true,
|
||||||
|
keyring: KeyringManager::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,10 +66,10 @@ impl ConfigManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Force save configuration
|
// /// Force save configuration
|
||||||
pub fn force_save(&self) -> Result<()> {
|
// pub fn force_save(&self) -> Result<()> {
|
||||||
self.config.save(&self.config_path)
|
// self.config.save(&self.config_path)
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Get configuration file path
|
/// Get configuration file path
|
||||||
pub fn path(&self) -> &Path {
|
pub fn path(&self) -> &Path {
|
||||||
@@ -101,11 +120,11 @@ impl ConfigManager {
|
|||||||
self.config.profiles.get(name)
|
self.config.profiles.get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get mutable profile
|
// /// Get mutable profile
|
||||||
pub fn get_profile_mut(&mut self, name: &str) -> Option<&mut GitProfile> {
|
// pub fn get_profile_mut(&mut self, name: &str) -> Option<&mut GitProfile> {
|
||||||
self.modified = true;
|
// self.modified = true;
|
||||||
self.config.profiles.get_mut(name)
|
// self.config.profiles.get_mut(name)
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// List all profile names
|
/// List all profile names
|
||||||
pub fn list_profiles(&self) -> Vec<&String> {
|
pub fn list_profiles(&self) -> Vec<&String> {
|
||||||
@@ -119,11 +138,11 @@ impl ConfigManager {
|
|||||||
|
|
||||||
/// Set default profile
|
/// Set default profile
|
||||||
pub fn set_default_profile(&mut self, name: Option<String>) -> Result<()> {
|
pub fn set_default_profile(&mut self, name: Option<String>) -> Result<()> {
|
||||||
if let Some(ref n) = name {
|
if let Some(ref n) = name
|
||||||
if !self.config.profiles.contains_key(n) {
|
&& !self.config.profiles.contains_key(n)
|
||||||
|
{
|
||||||
bail!("Profile '{}' does not exist", n);
|
bail!("Profile '{}' does not exist", n);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
self.config.default_profile = name;
|
self.config.default_profile = name;
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -153,54 +172,138 @@ impl ConfigManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get profile usage statistics
|
// /// Get profile usage statistics
|
||||||
pub fn get_profile_usage(&self, name: &str) -> Option<&super::UsageStats> {
|
// pub fn get_profile_usage(&self, name: &str) -> Option<&super::UsageStats> {
|
||||||
self.config.profiles.get(name).map(|p| &p.usage)
|
// self.config.profiles.get(name).map(|p| &p.usage)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Token management
|
// Token management
|
||||||
|
|
||||||
/// Add a token to a profile
|
/// Add a token to a profile (stores token in keyring)
|
||||||
pub fn add_token_to_profile(&mut self, profile_name: &str, service: String, token: TokenConfig) -> Result<()> {
|
pub fn add_token_to_profile(
|
||||||
|
&mut self,
|
||||||
|
profile_name: &str,
|
||||||
|
service: String,
|
||||||
|
token: TokenConfig,
|
||||||
|
) -> Result<()> {
|
||||||
|
if !self.config.profiles.contains_key(profile_name) {
|
||||||
|
bail!("Profile '{}' does not exist", profile_name);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
||||||
profile.add_token(service, token);
|
profile.add_token(service, token);
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Store a PAT token in keyring for a profile
|
||||||
|
pub fn store_pat_for_profile(
|
||||||
|
&self,
|
||||||
|
profile_name: &str,
|
||||||
|
service: &str,
|
||||||
|
token_value: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let profile = self
|
||||||
|
.get_profile(profile_name)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||||
|
|
||||||
|
let user_email = &profile.user_email;
|
||||||
|
|
||||||
|
self.keyring
|
||||||
|
.store_pat(profile_name, user_email, service, token_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a PAT token from keyring for a profile
|
||||||
|
pub fn get_pat_for_profile(&self, profile_name: &str, service: &str) -> Result<Option<String>> {
|
||||||
|
let profile = self
|
||||||
|
.get_profile(profile_name)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||||
|
|
||||||
|
let user_email = &profile.user_email;
|
||||||
|
|
||||||
|
self.keyring.get_pat(profile_name, user_email, service)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if a PAT token exists for a profile
|
||||||
|
pub fn has_pat_for_profile(&self, profile_name: &str, service: &str) -> bool {
|
||||||
|
if let Some(profile) = self.get_profile(profile_name) {
|
||||||
|
let user_email = &profile.user_email;
|
||||||
|
self.keyring.has_pat(profile_name, user_email, service)
|
||||||
} else {
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove a token from a profile (deletes from keyring)
|
||||||
|
pub fn remove_token_from_profile(&mut self, profile_name: &str, service: &str) -> Result<()> {
|
||||||
|
if !self.config.profiles.contains_key(profile_name) {
|
||||||
bail!("Profile '{}' does not exist", profile_name);
|
bail!("Profile '{}' does not exist", profile_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let user_email = self
|
||||||
|
.config
|
||||||
|
.profiles
|
||||||
|
.get(profile_name)
|
||||||
|
.unwrap()
|
||||||
|
.user_email
|
||||||
|
.clone();
|
||||||
|
let services: Vec<String> = self
|
||||||
|
.config
|
||||||
|
.profiles
|
||||||
|
.get(profile_name)
|
||||||
|
.unwrap()
|
||||||
|
.tokens
|
||||||
|
.keys()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !services.contains(&service.to_string()) {
|
||||||
|
bail!(
|
||||||
|
"Token for service '{}' not found in profile '{}'",
|
||||||
|
service,
|
||||||
|
profile_name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a token from a profile
|
self.keyring
|
||||||
pub fn get_token_from_profile(&self, profile_name: &str, service: &str) -> Option<&TokenConfig> {
|
.delete_pat(profile_name, &user_email, service)?;
|
||||||
self.config.profiles.get(profile_name)?.get_token(service)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove a token from a profile
|
|
||||||
pub fn remove_token_from_profile(&mut self, profile_name: &str, service: &str) -> Result<()> {
|
|
||||||
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
||||||
profile.remove_token(service);
|
profile.remove_token(service);
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
bail!("Profile '{}' does not exist", profile_name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List all tokens in a profile
|
Ok(())
|
||||||
pub fn list_profile_tokens(&self, profile_name: &str) -> Option<Vec<&String>> {
|
|
||||||
self.config.profiles.get(profile_name).map(|p| p.tokens.keys().collect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete all PAT tokens for a profile (used when removing a profile)
|
||||||
|
pub fn delete_all_pats_for_profile(&self, profile_name: &str) -> Result<()> {
|
||||||
|
if let Some(profile) = self.get_profile(profile_name) {
|
||||||
|
let user_email = &profile.user_email;
|
||||||
|
let services: Vec<String> = profile.tokens.keys().cloned().collect();
|
||||||
|
|
||||||
|
self.keyring
|
||||||
|
.delete_all_pats_for_profile(profile_name, user_email, &services)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// /// List all tokens in a profile
|
||||||
|
// pub fn list_profile_tokens(&self, profile_name: &str) -> Option<Vec<&String>> {
|
||||||
|
// self.config.profiles.get(profile_name).map(|p| p.tokens.keys().collect())
|
||||||
|
// }
|
||||||
|
|
||||||
// Repository profile management
|
// Repository profile management
|
||||||
|
|
||||||
/// Get profile for repository
|
// /// Get profile for repository
|
||||||
pub fn get_repo_profile(&self, repo_path: &str) -> Option<&GitProfile> {
|
// pub fn get_repo_profile(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||||
self.config
|
// self.config
|
||||||
.repo_profiles
|
// .repo_profiles
|
||||||
.get(repo_path)
|
// .get(repo_path)
|
||||||
.and_then(|name| self.config.profiles.get(name))
|
// .and_then(|name| self.config.profiles.get(name))
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Set profile for repository
|
/// Set profile for repository
|
||||||
pub fn set_repo_profile(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
pub fn set_repo_profile(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
||||||
@@ -212,32 +315,75 @@ impl ConfigManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove repository profile mapping
|
// /// Remove repository profile mapping
|
||||||
pub fn remove_repo_profile(&mut self, repo_path: &str) {
|
// pub fn remove_repo_profile(&mut self, repo_path: &str) {
|
||||||
self.config.repo_profiles.remove(repo_path);
|
// self.config.repo_profiles.remove(repo_path);
|
||||||
self.modified = true;
|
// self.modified = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// /// List repository profile mappings
|
||||||
|
// pub fn list_repo_profiles(&self) -> &HashMap<String, String> {
|
||||||
|
// &self.config.repo_profiles
|
||||||
|
// }
|
||||||
|
|
||||||
|
// /// Get effective profile for a repository (repo-specific -> default)
|
||||||
|
// pub fn get_effective_profile(&self, repo_path: Option<&str>) -> Option<&GitProfile> {
|
||||||
|
// if let Some(path) = repo_path {
|
||||||
|
// if let Some(profile) = self.get_repo_profile(path) {
|
||||||
|
// return Some(profile);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// self.default_profile()
|
||||||
|
// }
|
||||||
|
|
||||||
|
/// Check and compare profile with git configuration
|
||||||
|
pub fn check_profile_config(
|
||||||
|
&self,
|
||||||
|
profile_name: &str,
|
||||||
|
repo: &git2::Repository,
|
||||||
|
) -> Result<super::ProfileComparison> {
|
||||||
|
let profile = self
|
||||||
|
.get_profile(profile_name)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||||
|
profile.compare_with_git_config(repo)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List repository profile mappings
|
/// Find a profile that matches the given user config (name, email, signing_key)
|
||||||
pub fn list_repo_profiles(&self) -> &HashMap<String, String> {
|
pub fn find_matching_profile(
|
||||||
&self.config.repo_profiles
|
&self,
|
||||||
}
|
user_name: &str,
|
||||||
|
user_email: &str,
|
||||||
|
signing_key: Option<&str>,
|
||||||
|
) -> Option<&GitProfile> {
|
||||||
|
for profile in self.config.profiles.values() {
|
||||||
|
let name_match = profile.user_name == user_name;
|
||||||
|
let email_match = profile.user_email == user_email;
|
||||||
|
let key_match = match (signing_key, profile.signing_key()) {
|
||||||
|
(Some(git_key), Some(profile_key)) => git_key == profile_key,
|
||||||
|
(None, None) => true,
|
||||||
|
(Some(_), None) => false,
|
||||||
|
(None, Some(_)) => false,
|
||||||
|
};
|
||||||
|
|
||||||
/// Get effective profile for a repository (repo-specific -> default)
|
if name_match && email_match && key_match {
|
||||||
pub fn get_effective_profile(&self, repo_path: Option<&str>) -> Option<&GitProfile> {
|
|
||||||
if let Some(path) = repo_path {
|
|
||||||
if let Some(profile) = self.get_repo_profile(path) {
|
|
||||||
return Some(profile);
|
return Some(profile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.default_profile()
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check and compare profile with git configuration
|
/// Find profiles that partially match (same name or same email)
|
||||||
pub fn check_profile_config(&self, profile_name: &str, repo: &git2::Repository) -> Result<super::ProfileComparison> {
|
pub fn find_partial_matches(&self, user_name: &str, user_email: &str) -> Vec<&GitProfile> {
|
||||||
let profile = self.get_profile(profile_name)
|
self.config
|
||||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
.profiles
|
||||||
profile.compare_with_git_config(repo)
|
.values()
|
||||||
|
.filter(|p| p.user_name == user_name || p.user_email == user_email)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get repo profile mapping
|
||||||
|
pub fn get_repo_profile_name(&self, repo_path: &str) -> Option<&String> {
|
||||||
|
self.config.repo_profiles.get(repo_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LLM configuration
|
// LLM configuration
|
||||||
@@ -249,104 +395,169 @@ impl ConfigManager {
|
|||||||
|
|
||||||
/// Set LLM provider
|
/// Set LLM provider
|
||||||
pub fn set_llm_provider(&mut self, provider: String) {
|
pub fn set_llm_provider(&mut self, provider: String) {
|
||||||
self.config.llm.provider = provider;
|
let default_model = get_default_model(&provider);
|
||||||
|
self.config.llm.provider = provider.clone();
|
||||||
|
if self.config.llm.model.is_empty() || self.config.llm.model == "llama2" {
|
||||||
|
self.config.llm.model = default_model.to_string();
|
||||||
|
}
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get OpenAI API key
|
/// Get model
|
||||||
pub fn openai_api_key(&self) -> Option<&String> {
|
pub fn llm_model(&self) -> &str {
|
||||||
self.config.llm.openai.api_key.as_ref()
|
&self.config.llm.model
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set OpenAI API key
|
/// Set model
|
||||||
pub fn set_openai_api_key(&mut self, key: String) {
|
pub fn set_llm_model(&mut self, model: String) {
|
||||||
self.config.llm.openai.api_key = Some(key);
|
self.config.llm.model = model;
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Anthropic API key
|
/// Get base URL (returns provider default if not set)
|
||||||
pub fn anthropic_api_key(&self) -> Option<&String> {
|
pub fn llm_base_url(&self) -> String {
|
||||||
self.config.llm.anthropic.api_key.as_ref()
|
match &self.config.llm.base_url {
|
||||||
|
Some(url) => url.clone(),
|
||||||
|
None => get_default_base_url(&self.config.llm.provider).to_string(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set Anthropic API key
|
/// Set base URL
|
||||||
pub fn set_anthropic_api_key(&mut self, key: String) {
|
pub fn set_llm_base_url(&mut self, url: Option<String>) {
|
||||||
self.config.llm.anthropic.api_key = Some(key);
|
self.config.llm.base_url = url;
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Kimi API key
|
/// Get API key from configured storage method
|
||||||
pub fn kimi_api_key(&self) -> Option<&String> {
|
pub fn get_api_key(&self) -> Option<String> {
|
||||||
self.config.llm.kimi.api_key.as_ref()
|
// First try environment variables (always checked)
|
||||||
|
if let Some(key) = self
|
||||||
|
.keyring
|
||||||
|
.get_api_key(&self.config.llm.provider)
|
||||||
|
.unwrap_or(None)
|
||||||
|
{
|
||||||
|
return Some(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set Kimi API key
|
// Then try config file if configured
|
||||||
pub fn set_kimi_api_key(&mut self, key: String) {
|
if self.config.llm.api_key_storage == "config" {
|
||||||
self.config.llm.kimi.api_key = Some(key);
|
return self.config.llm.api_key.clone();
|
||||||
self.modified = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Kimi base URL
|
None
|
||||||
pub fn kimi_base_url(&self) -> &str {
|
|
||||||
&self.config.llm.kimi.base_url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set Kimi base URL
|
/// Store API key in configured storage method
|
||||||
pub fn set_kimi_base_url(&mut self, url: String) {
|
pub fn set_api_key(&self, api_key: &str) -> Result<()> {
|
||||||
self.config.llm.kimi.base_url = url;
|
match self.config.llm.api_key_storage.as_str() {
|
||||||
self.modified = true;
|
"keyring" => {
|
||||||
|
if !self.keyring.is_available() {
|
||||||
|
bail!(
|
||||||
|
"Keyring is not available. Set QUICOMMIT_API_KEY environment variable instead or change api_key_storage to 'config'."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
self.keyring
|
||||||
|
.store_api_key(&self.config.llm.provider, api_key)
|
||||||
|
}
|
||||||
|
"config" => {
|
||||||
|
// We can't modify self.config here since self is immutable
|
||||||
|
// This will be handled by the caller updating the config
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
"environment" => {
|
||||||
|
bail!(
|
||||||
|
"API key storage set to 'environment'. Please set QUICOMMIT_{}_API_KEY environment variable.",
|
||||||
|
self.config.llm.provider.to_uppercase()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
bail!(
|
||||||
|
"Invalid API key storage method: {}",
|
||||||
|
self.config.llm.api_key_storage
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get DeepSeek API key
|
/// Delete API key from configured storage method
|
||||||
pub fn deepseek_api_key(&self) -> Option<&String> {
|
pub fn delete_api_key(&self) -> Result<()> {
|
||||||
self.config.llm.deepseek.api_key.as_ref()
|
match self.config.llm.api_key_storage.as_str() {
|
||||||
|
"keyring" => {
|
||||||
|
if self.keyring.is_available() {
|
||||||
|
self.keyring.delete_api_key(&self.config.llm.provider)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"config" => {
|
||||||
|
// We can't modify self.config here since self is immutable
|
||||||
|
// This will be handled by the caller updating the config
|
||||||
|
}
|
||||||
|
"environment" => {
|
||||||
|
// Environment variables are not managed by the app
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
bail!(
|
||||||
|
"Invalid API key storage method: {}",
|
||||||
|
self.config.llm.api_key_storage
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set DeepSeek API key
|
/// Check if API key is configured
|
||||||
pub fn set_deepseek_api_key(&mut self, key: String) {
|
pub fn has_api_key(&self) -> bool {
|
||||||
self.config.llm.deepseek.api_key = Some(key);
|
if !provider_needs_api_key(&self.config.llm.provider) {
|
||||||
self.modified = true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get DeepSeek base URL
|
// Check environment variables
|
||||||
pub fn deepseek_base_url(&self) -> &str {
|
if self
|
||||||
&self.config.llm.deepseek.base_url
|
.keyring
|
||||||
|
.get_api_key(&self.config.llm.provider)
|
||||||
|
.unwrap_or(None)
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set DeepSeek base URL
|
// Check config file if configured
|
||||||
pub fn set_deepseek_base_url(&mut self, url: String) {
|
if self.config.llm.api_key_storage == "config" {
|
||||||
self.config.llm.deepseek.base_url = url;
|
return self.config.llm.api_key.is_some();
|
||||||
self.modified = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get OpenRouter API key
|
false
|
||||||
pub fn openrouter_api_key(&self) -> Option<&String> {
|
|
||||||
self.config.llm.openrouter.api_key.as_ref()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set OpenRouter API key
|
/// Get keyring manager reference
|
||||||
pub fn set_openrouter_api_key(&mut self, key: String) {
|
pub fn keyring(&self) -> &KeyringManager {
|
||||||
self.config.llm.openrouter.api_key = Some(key);
|
&self.keyring
|
||||||
self.modified = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get OpenRouter base URL
|
// /// Configure LLM provider with all settings
|
||||||
pub fn openrouter_base_url(&self) -> &str {
|
// pub fn configure_llm(&mut self, provider: String, model: Option<String>, base_url: Option<String>, api_key: Option<&str>) -> Result<()> {
|
||||||
&self.config.llm.openrouter.base_url
|
// self.set_llm_provider(provider.clone());
|
||||||
}
|
|
||||||
|
|
||||||
/// Set OpenRouter base URL
|
// if let Some(m) = model {
|
||||||
pub fn set_openrouter_base_url(&mut self, url: String) {
|
// self.set_llm_model(m);
|
||||||
self.config.llm.openrouter.base_url = url;
|
// }
|
||||||
self.modified = true;
|
|
||||||
}
|
// self.set_llm_base_url(base_url);
|
||||||
|
|
||||||
|
// if let Some(key) = api_key {
|
||||||
|
// if provider_needs_api_key(&provider) {
|
||||||
|
// self.set_api_key(key)?;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
|
||||||
// Commit configuration
|
// Commit configuration
|
||||||
|
|
||||||
/// Get commit format
|
// /// Get commit format
|
||||||
pub fn commit_format(&self) -> super::CommitFormat {
|
// pub fn commit_format(&self) -> super::CommitFormat {
|
||||||
self.config.commit.format
|
// self.config.commit.format
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Set commit format
|
/// Set commit format
|
||||||
pub fn set_commit_format(&mut self, format: super::CommitFormat) {
|
pub fn set_commit_format(&mut self, format: super::CommitFormat) {
|
||||||
@@ -354,10 +565,10 @@ impl ConfigManager {
|
|||||||
self.modified = true;
|
self.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if auto-generate is enabled
|
// /// Check if auto-generate is enabled
|
||||||
pub fn auto_generate_commits(&self) -> bool {
|
// pub fn auto_generate_commits(&self) -> bool {
|
||||||
self.config.commit.auto_generate
|
// self.config.commit.auto_generate
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Set auto-generate commits
|
/// Set auto-generate commits
|
||||||
pub fn set_auto_generate_commits(&mut self, enabled: bool) {
|
pub fn set_auto_generate_commits(&mut self, enabled: bool) {
|
||||||
@@ -367,10 +578,10 @@ impl ConfigManager {
|
|||||||
|
|
||||||
// Tag configuration
|
// Tag configuration
|
||||||
|
|
||||||
/// Get version prefix
|
// /// Get version prefix
|
||||||
pub fn version_prefix(&self) -> &str {
|
// pub fn version_prefix(&self) -> &str {
|
||||||
&self.config.tag.version_prefix
|
// &self.config.tag.version_prefix
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Set version prefix
|
/// Set version prefix
|
||||||
pub fn set_version_prefix(&mut self, prefix: String) {
|
pub fn set_version_prefix(&mut self, prefix: String) {
|
||||||
@@ -380,10 +591,10 @@ impl ConfigManager {
|
|||||||
|
|
||||||
// Changelog configuration
|
// Changelog configuration
|
||||||
|
|
||||||
/// Get changelog path
|
// /// Get changelog path
|
||||||
pub fn changelog_path(&self) -> &str {
|
// pub fn changelog_path(&self) -> &str {
|
||||||
&self.config.changelog.path
|
// &self.config.changelog.path
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Set changelog path
|
/// Set changelog path
|
||||||
pub fn set_changelog_path(&mut self, path: String) {
|
pub fn set_changelog_path(&mut self, path: String) {
|
||||||
@@ -391,16 +602,54 @@ impl ConfigManager {
|
|||||||
self.modified = true;
|
self.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Language configuration
|
||||||
|
|
||||||
|
// /// Get output language
|
||||||
|
// pub fn output_language(&self) -> &str {
|
||||||
|
// &self.config.language.output_language
|
||||||
|
// }
|
||||||
|
|
||||||
|
/// Set output language
|
||||||
|
pub fn set_output_language(&mut self, language: String) {
|
||||||
|
self.config.language.output_language = language;
|
||||||
|
self.modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get language enum from config
|
||||||
|
pub fn get_language(&self) -> Option<super::Language> {
|
||||||
|
super::Language::from_str(&self.config.language.output_language)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if commit types should be kept in English
|
||||||
|
pub fn keep_types_english(&self) -> bool {
|
||||||
|
self.config.language.keep_types_english
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set keep types English flag
|
||||||
|
pub fn set_keep_types_english(&mut self, keep: bool) {
|
||||||
|
self.config.language.keep_types_english = keep;
|
||||||
|
self.modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if changelog types should be kept in English
|
||||||
|
pub fn keep_changelog_types_english(&self) -> bool {
|
||||||
|
self.config.language.keep_changelog_types_english
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set keep changelog types English flag
|
||||||
|
pub fn set_keep_changelog_types_english(&mut self, keep: bool) {
|
||||||
|
self.config.language.keep_changelog_types_english = keep;
|
||||||
|
self.modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// Export configuration to TOML string
|
/// Export configuration to TOML string
|
||||||
pub fn export(&self) -> Result<String> {
|
pub fn export(&self) -> Result<String> {
|
||||||
toml::to_string_pretty(&self.config)
|
toml::to_string_pretty(&self.config).context("Failed to serialize config")
|
||||||
.context("Failed to serialize config")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Import configuration from TOML string
|
/// Import configuration from TOML string
|
||||||
pub fn import(&mut self, toml_str: &str) -> Result<()> {
|
pub fn import(&mut self, toml_str: &str) -> Result<()> {
|
||||||
self.config = toml::from_str(toml_str)
|
self.config = toml::from_str(toml_str).context("Failed to parse config")?;
|
||||||
.context("Failed to parse config")?;
|
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -418,6 +667,7 @@ impl Default for ConfigManager {
|
|||||||
config: AppConfig::default(),
|
config: AppConfig::default(),
|
||||||
config_path: PathBuf::new(),
|
config_path: PathBuf::new(),
|
||||||
modified: false,
|
modified: false,
|
||||||
|
keyring: KeyringManager::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,7 @@ use std::path::{Path, PathBuf};
|
|||||||
pub mod manager;
|
pub mod manager;
|
||||||
pub mod profile;
|
pub mod profile;
|
||||||
|
|
||||||
pub use manager::ConfigManager;
|
pub use profile::{GitProfile, ProfileComparison, TokenConfig, TokenType};
|
||||||
pub use profile::{
|
|
||||||
GitProfile, ProfileSettings, SshConfig, GpgConfig, TokenConfig, TokenType,
|
|
||||||
UsageStats, ProfileComparison, ConfigDifference
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Application configuration
|
/// Application configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -54,6 +50,10 @@ pub struct AppConfig {
|
|||||||
/// Theme settings
|
/// Theme settings
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub theme: ThemeConfig,
|
pub theme: ThemeConfig,
|
||||||
|
|
||||||
|
/// Language settings
|
||||||
|
#[serde(default)]
|
||||||
|
pub language: LanguageConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppConfig {
|
impl Default for AppConfig {
|
||||||
@@ -69,6 +69,7 @@ impl Default for AppConfig {
|
|||||||
repo_profiles: HashMap::new(),
|
repo_profiles: HashMap::new(),
|
||||||
encrypt_sensitive: true,
|
encrypt_sensitive: true,
|
||||||
theme: ThemeConfig::default(),
|
theme: ThemeConfig::default(),
|
||||||
|
language: LanguageConfig::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,37 +77,16 @@ impl Default for AppConfig {
|
|||||||
/// LLM configuration
|
/// LLM configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct LlmConfig {
|
pub struct LlmConfig {
|
||||||
/// Default LLM provider
|
/// Current LLM provider (ollama, openai, anthropic, kimi, deepseek, openrouter)
|
||||||
#[serde(default = "default_llm_provider")]
|
#[serde(default = "default_llm_provider")]
|
||||||
pub provider: String,
|
pub provider: String,
|
||||||
|
|
||||||
/// OpenAI configuration
|
/// Model to use (stored in config, not in keyring)
|
||||||
#[serde(default)]
|
#[serde(default = "default_model")]
|
||||||
pub openai: OpenAiConfig,
|
pub model: String,
|
||||||
|
|
||||||
/// Ollama configuration
|
/// API base URL (optional, will use provider default if not set)
|
||||||
#[serde(default)]
|
pub base_url: Option<String>,
|
||||||
pub ollama: OllamaConfig,
|
|
||||||
|
|
||||||
/// Anthropic Claude configuration
|
|
||||||
#[serde(default)]
|
|
||||||
pub anthropic: AnthropicConfig,
|
|
||||||
|
|
||||||
/// Kimi (Moonshot AI) configuration
|
|
||||||
#[serde(default)]
|
|
||||||
pub kimi: KimiConfig,
|
|
||||||
|
|
||||||
/// DeepSeek configuration
|
|
||||||
#[serde(default)]
|
|
||||||
pub deepseek: DeepSeekConfig,
|
|
||||||
|
|
||||||
/// OpenRouter configuration
|
|
||||||
#[serde(default)]
|
|
||||||
pub openrouter: OpenRouterConfig,
|
|
||||||
|
|
||||||
/// Custom API configuration
|
|
||||||
#[serde(default)]
|
|
||||||
pub custom: Option<CustomLlmConfig>,
|
|
||||||
|
|
||||||
/// Maximum tokens for generation
|
/// Maximum tokens for generation
|
||||||
#[serde(default = "default_max_tokens")]
|
#[serde(default = "default_max_tokens")]
|
||||||
@@ -119,186 +99,45 @@ pub struct LlmConfig {
|
|||||||
/// Timeout in seconds
|
/// Timeout in seconds
|
||||||
#[serde(default = "default_timeout")]
|
#[serde(default = "default_timeout")]
|
||||||
pub timeout: u64,
|
pub timeout: u64,
|
||||||
|
|
||||||
|
/// API key storage method (keyring, config, environment)
|
||||||
|
#[serde(default = "default_api_key_storage")]
|
||||||
|
pub api_key_storage: String,
|
||||||
|
|
||||||
|
/// API key (stored in config for fallback, encrypted if encrypt_sensitive is true)
|
||||||
|
#[serde(default)]
|
||||||
|
pub api_key: Option<String>,
|
||||||
|
|
||||||
|
/// Enable thinking/reasoning mode (deepseek, kimi, anthropic)
|
||||||
|
#[serde(default)]
|
||||||
|
pub thinking_enabled: bool,
|
||||||
|
|
||||||
|
/// Budget tokens for thinking mode (Anthropic Claude 4)
|
||||||
|
#[serde(default)]
|
||||||
|
pub thinking_budget_tokens: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_api_key_storage() -> String {
|
||||||
|
"keyring".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for LlmConfig {
|
impl Default for LlmConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
provider: default_llm_provider(),
|
provider: default_llm_provider(),
|
||||||
openai: OpenAiConfig::default(),
|
model: default_model(),
|
||||||
ollama: OllamaConfig::default(),
|
base_url: None,
|
||||||
anthropic: AnthropicConfig::default(),
|
|
||||||
kimi: KimiConfig::default(),
|
|
||||||
deepseek: DeepSeekConfig::default(),
|
|
||||||
openrouter: OpenRouterConfig::default(),
|
|
||||||
custom: None,
|
|
||||||
max_tokens: default_max_tokens(),
|
max_tokens: default_max_tokens(),
|
||||||
temperature: default_temperature(),
|
temperature: default_temperature(),
|
||||||
timeout: default_timeout(),
|
timeout: default_timeout(),
|
||||||
}
|
api_key_storage: default_api_key_storage(),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// OpenAI API configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct OpenAiConfig {
|
|
||||||
/// API key
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_openai_model")]
|
|
||||||
pub model: String,
|
|
||||||
|
|
||||||
/// API base URL (for custom endpoints)
|
|
||||||
#[serde(default = "default_openai_base_url")]
|
|
||||||
pub base_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for OpenAiConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
api_key: None,
|
api_key: None,
|
||||||
model: default_openai_model(),
|
thinking_enabled: false,
|
||||||
base_url: default_openai_base_url(),
|
thinking_budget_tokens: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ollama configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct OllamaConfig {
|
|
||||||
/// Ollama server URL
|
|
||||||
#[serde(default = "default_ollama_url")]
|
|
||||||
pub url: String,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_ollama_model")]
|
|
||||||
pub model: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for OllamaConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
url: default_ollama_url(),
|
|
||||||
model: default_ollama_model(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Anthropic Claude configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct AnthropicConfig {
|
|
||||||
/// API key
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_anthropic_model")]
|
|
||||||
pub model: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for AnthropicConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
api_key: None,
|
|
||||||
model: default_anthropic_model(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Kimi (Moonshot AI) configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct KimiConfig {
|
|
||||||
/// API key
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_kimi_model")]
|
|
||||||
pub model: String,
|
|
||||||
|
|
||||||
/// API base URL (for custom endpoints)
|
|
||||||
#[serde(default = "default_kimi_base_url")]
|
|
||||||
pub base_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for KimiConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
api_key: None,
|
|
||||||
model: default_kimi_model(),
|
|
||||||
base_url: default_kimi_base_url(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// DeepSeek configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct DeepSeekConfig {
|
|
||||||
/// API key
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_deepseek_model")]
|
|
||||||
pub model: String,
|
|
||||||
|
|
||||||
/// API base URL (for custom endpoints)
|
|
||||||
#[serde(default = "default_deepseek_base_url")]
|
|
||||||
pub base_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for DeepSeekConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
api_key: None,
|
|
||||||
model: default_deepseek_model(),
|
|
||||||
base_url: default_deepseek_base_url(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// OpenRouter configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct OpenRouterConfig {
|
|
||||||
/// API key
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model to use
|
|
||||||
#[serde(default = "default_openrouter_model")]
|
|
||||||
pub model: String,
|
|
||||||
|
|
||||||
/// API base URL (for custom endpoints)
|
|
||||||
#[serde(default = "default_openrouter_base_url")]
|
|
||||||
pub base_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for OpenRouterConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
api_key: None,
|
|
||||||
model: default_openrouter_model(),
|
|
||||||
base_url: default_openrouter_base_url(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Custom LLM API configuration
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct CustomLlmConfig {
|
|
||||||
/// API endpoint URL
|
|
||||||
pub url: String,
|
|
||||||
|
|
||||||
/// API key (optional)
|
|
||||||
pub api_key: Option<String>,
|
|
||||||
|
|
||||||
/// Model name
|
|
||||||
pub model: String,
|
|
||||||
|
|
||||||
/// Request format template (JSON)
|
|
||||||
pub request_template: String,
|
|
||||||
|
|
||||||
/// Response path to extract content (e.g., "choices.0.message.content")
|
|
||||||
pub response_path: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Commit configuration
|
/// Commit configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct CommitConfig {
|
pub struct CommitConfig {
|
||||||
@@ -498,6 +337,83 @@ impl Default for ThemeConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Language configuration
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct LanguageConfig {
|
||||||
|
/// Output language for messages (en, zh, etc.)
|
||||||
|
#[serde(default = "default_output_language")]
|
||||||
|
pub output_language: String,
|
||||||
|
|
||||||
|
/// Keep commit types in English
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub keep_types_english: bool,
|
||||||
|
|
||||||
|
/// Keep changelog types in English
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub keep_changelog_types_english: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LanguageConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
output_language: default_output_language(),
|
||||||
|
keep_types_english: true,
|
||||||
|
keep_changelog_types_english: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Supported languages
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Language {
|
||||||
|
English,
|
||||||
|
Chinese,
|
||||||
|
Japanese,
|
||||||
|
Korean,
|
||||||
|
Spanish,
|
||||||
|
French,
|
||||||
|
German,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Language {
|
||||||
|
pub fn from_str(s: &str) -> Option<Self> {
|
||||||
|
match s.to_lowercase().as_str() {
|
||||||
|
"en" | "english" => Some(Language::English),
|
||||||
|
"zh" | "chinese" | "zh-cn" | "zh-tw" => Some(Language::Chinese),
|
||||||
|
"ja" | "japanese" => Some(Language::Japanese),
|
||||||
|
"ko" | "korean" => Some(Language::Korean),
|
||||||
|
"es" | "spanish" => Some(Language::Spanish),
|
||||||
|
"fr" | "french" => Some(Language::French),
|
||||||
|
"de" | "german" => Some(Language::German),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_code(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Language::English => "en",
|
||||||
|
Language::Chinese => "zh",
|
||||||
|
Language::Japanese => "ja",
|
||||||
|
Language::Korean => "ko",
|
||||||
|
Language::Spanish => "es",
|
||||||
|
Language::French => "fr",
|
||||||
|
Language::German => "de",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn display_name(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Language::English => "English",
|
||||||
|
Language::Chinese => "中文",
|
||||||
|
Language::Japanese => "日本語",
|
||||||
|
Language::Korean => "한국어",
|
||||||
|
Language::Spanish => "Español",
|
||||||
|
Language::French => "Français",
|
||||||
|
Language::German => "Deutsch",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Default value functions
|
// Default value functions
|
||||||
fn default_version() -> String {
|
fn default_version() -> String {
|
||||||
"1".to_string()
|
"1".to_string()
|
||||||
@@ -511,6 +427,10 @@ fn default_llm_provider() -> String {
|
|||||||
"ollama".to_string()
|
"ollama".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_model() -> String {
|
||||||
|
"llama2".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
fn default_max_tokens() -> u32 {
|
fn default_max_tokens() -> u32 {
|
||||||
500
|
500
|
||||||
}
|
}
|
||||||
@@ -523,50 +443,6 @@ fn default_timeout() -> u64 {
|
|||||||
30
|
30
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_openai_model() -> String {
|
|
||||||
"gpt-4".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_openai_base_url() -> String {
|
|
||||||
"https://api.openai.com/v1".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_ollama_url() -> String {
|
|
||||||
"http://localhost:11434".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_ollama_model() -> String {
|
|
||||||
"llama2".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_anthropic_model() -> String {
|
|
||||||
"claude-3-sonnet-20240229".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_kimi_model() -> String {
|
|
||||||
"moonshot-v1-8k".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_kimi_base_url() -> String {
|
|
||||||
"https://api.moonshot.cn/v1".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_deepseek_model() -> String {
|
|
||||||
"deepseek-chat".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_deepseek_base_url() -> String {
|
|
||||||
"https://api.deepseek.com/v1".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_openrouter_model() -> String {
|
|
||||||
"openai/gpt-3.5-turbo".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_openrouter_base_url() -> String {
|
|
||||||
"https://openrouter.ai/api/v1".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_commit_format() -> CommitFormat {
|
fn default_commit_format() -> CommitFormat {
|
||||||
CommitFormat::Conventional
|
CommitFormat::Conventional
|
||||||
}
|
}
|
||||||
@@ -595,6 +471,10 @@ fn default_date_format() -> String {
|
|||||||
"%Y-%m-%d".to_string()
|
"%Y-%m-%d".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_output_language() -> String {
|
||||||
|
"en".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
impl AppConfig {
|
impl AppConfig {
|
||||||
/// Load configuration from file
|
/// Load configuration from file
|
||||||
pub fn load(path: &Path) -> Result<Self> {
|
pub fn load(path: &Path) -> Result<Self> {
|
||||||
@@ -611,8 +491,7 @@ impl AppConfig {
|
|||||||
|
|
||||||
/// Save configuration to file
|
/// Save configuration to file
|
||||||
pub fn save(&self, path: &Path) -> Result<()> {
|
pub fn save(&self, path: &Path) -> Result<()> {
|
||||||
let content = toml::to_string_pretty(self)
|
let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
|
||||||
.context("Failed to serialize config")?;
|
|
||||||
|
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
fs::create_dir_all(parent)
|
fs::create_dir_all(parent)
|
||||||
@@ -627,23 +506,74 @@ impl AppConfig {
|
|||||||
|
|
||||||
/// Get default config path
|
/// Get default config path
|
||||||
pub fn default_path() -> Result<PathBuf> {
|
pub fn default_path() -> Result<PathBuf> {
|
||||||
let config_dir = dirs::config_dir()
|
let config_dir = dirs::config_dir().context("Could not find config directory")?;
|
||||||
.context("Could not find config directory")?;
|
|
||||||
Ok(config_dir.join("quicommit").join("config.toml"))
|
Ok(config_dir.join("quicommit").join("config.toml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get profile for a repository
|
// /// Get profile for a repository
|
||||||
pub fn get_profile_for_repo(&self, repo_path: &str) -> Option<&GitProfile> {
|
// pub fn get_profile_for_repo(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||||
let profile_name = self.repo_profiles.get(repo_path)?;
|
// let profile_name = self.repo_profiles.get(repo_path)?;
|
||||||
self.profiles.get(profile_name)
|
// self.profiles.get(profile_name)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// /// Set profile for a repository
|
||||||
|
// pub fn set_profile_for_repo(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
||||||
|
// if !self.profiles.contains_key(&profile_name) {
|
||||||
|
// anyhow::bail!("Profile '{}' does not exist", profile_name);
|
||||||
|
// }
|
||||||
|
// self.repo_profiles.insert(repo_path, profile_name);
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encrypted PAT data for export
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct EncryptedPat {
|
||||||
|
/// Profile name
|
||||||
|
pub profile_name: String,
|
||||||
|
/// Service name (e.g., github, gitlab)
|
||||||
|
pub service: String,
|
||||||
|
/// User email (for keyring lookup)
|
||||||
|
pub user_email: String,
|
||||||
|
/// Encrypted token value
|
||||||
|
pub encrypted_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Export data container with optional encrypted PATs
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ExportData {
|
||||||
|
/// Configuration content (TOML string)
|
||||||
|
pub config: String,
|
||||||
|
/// Encrypted PATs (only present when exporting with encryption)
|
||||||
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||||
|
pub encrypted_pats: Vec<EncryptedPat>,
|
||||||
|
/// Export version for future compatibility
|
||||||
|
#[serde(default = "default_export_version")]
|
||||||
|
pub export_version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_export_version() -> String {
|
||||||
|
"1".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExportData {
|
||||||
|
pub fn new(config: String) -> Self {
|
||||||
|
Self {
|
||||||
|
config,
|
||||||
|
encrypted_pats: Vec::new(),
|
||||||
|
export_version: default_export_version(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set profile for a repository
|
pub fn with_encrypted_pats(config: String, pats: Vec<EncryptedPat>) -> Self {
|
||||||
pub fn set_profile_for_repo(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
Self {
|
||||||
if !self.profiles.contains_key(&profile_name) {
|
config,
|
||||||
anyhow::bail!("Profile '{}' does not exist", profile_name);
|
encrypted_pats: pats,
|
||||||
|
export_version: default_export_version(),
|
||||||
}
|
}
|
||||||
self.repo_profiles.insert(repo_path, profile_name);
|
}
|
||||||
Ok(())
|
|
||||||
|
pub fn has_encrypted_pats(&self) -> bool {
|
||||||
|
!self.encrypted_pats.is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{Result, bail};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@@ -120,8 +120,7 @@ impl GitProfile {
|
|||||||
/// Get signing key (from GPG config or direct)
|
/// Get signing key (from GPG config or direct)
|
||||||
pub fn signing_key(&self) -> Option<&str> {
|
pub fn signing_key(&self) -> Option<&str> {
|
||||||
self.signing_key
|
self.signing_key
|
||||||
.as_ref()
|
.as_deref()
|
||||||
.map(|s| s.as_str())
|
|
||||||
.or_else(|| self.gpg.as_ref().map(|g| g.key_id.as_str()))
|
.or_else(|| self.gpg.as_ref().map(|g| g.key_id.as_str()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,10 +174,20 @@ impl GitProfile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref ssh) = self.ssh {
|
if let Some(ref ssh) = self.ssh
|
||||||
if let Some(ref key_path) = ssh.private_key_path {
|
&& let Some(ref key_path) = ssh.private_key_path
|
||||||
config.set_str("core.sshCommand",
|
{
|
||||||
&format!("ssh -i {}", key_path.display()))?;
|
let path_str = key_path.display().to_string();
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
config.set_str(
|
||||||
|
"core.sshCommand",
|
||||||
|
&format!("ssh -i \"{}\"", path_str.replace('\\', "/")),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
{
|
||||||
|
config.set_str("core.sshCommand", &format!("ssh -i '{}'", path_str))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,6 +203,31 @@ impl GitProfile {
|
|||||||
|
|
||||||
if let Some(key) = self.signing_key() {
|
if let Some(key) = self.signing_key() {
|
||||||
config.set_str("user.signingkey", key)?;
|
config.set_str("user.signingkey", key)?;
|
||||||
|
|
||||||
|
if self.settings.auto_sign_commits {
|
||||||
|
config.set_bool("commit.gpgsign", true)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.settings.auto_sign_tags {
|
||||||
|
config.set_bool("tag.gpgsign", true)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref ssh) = self.ssh
|
||||||
|
&& let Some(ref key_path) = ssh.private_key_path
|
||||||
|
{
|
||||||
|
let path_str = key_path.display().to_string();
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
config.set_str(
|
||||||
|
"core.sshCommand",
|
||||||
|
&format!("ssh -i \"{}\"", path_str.replace('\\', "/")),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
{
|
||||||
|
config.set_str("core.sshCommand", &format!("ssh -i '{}'", path_str))?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -231,8 +265,9 @@ impl GitProfile {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(profile_key) = self.signing_key() {
|
if let Some(profile_key) = self.signing_key()
|
||||||
if git_signing_key.as_deref() != Some(profile_key) {
|
&& git_signing_key.as_deref() != Some(profile_key)
|
||||||
|
{
|
||||||
comparison.matches = false;
|
comparison.matches = false;
|
||||||
comparison.differences.push(ConfigDifference {
|
comparison.differences.push(ConfigDifference {
|
||||||
key: "user.signingkey".to_string(),
|
key: "user.signingkey".to_string(),
|
||||||
@@ -240,14 +275,13 @@ impl GitProfile {
|
|||||||
git_value: git_signing_key.unwrap_or_else(|| "<not set>".to_string()),
|
git_value: git_signing_key.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(comparison)
|
Ok(comparison)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Profile settings
|
/// Profile settings
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
pub struct ProfileSettings {
|
pub struct ProfileSettings {
|
||||||
/// Automatically sign commits
|
/// Automatically sign commits
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@@ -274,19 +308,6 @@ pub struct ProfileSettings {
|
|||||||
pub commit_template: Option<String>,
|
pub commit_template: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ProfileSettings {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
auto_sign_commits: false,
|
|
||||||
auto_sign_tags: false,
|
|
||||||
default_commit_format: None,
|
|
||||||
repo_patterns: vec![],
|
|
||||||
llm_provider: None,
|
|
||||||
commit_template: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// SSH configuration
|
/// SSH configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct SshConfig {
|
pub struct SshConfig {
|
||||||
@@ -316,17 +337,17 @@ pub struct SshConfig {
|
|||||||
impl SshConfig {
|
impl SshConfig {
|
||||||
/// Validate SSH configuration
|
/// Validate SSH configuration
|
||||||
pub fn validate(&self) -> Result<()> {
|
pub fn validate(&self) -> Result<()> {
|
||||||
if let Some(ref path) = self.private_key_path {
|
if let Some(ref path) = self.private_key_path
|
||||||
if !path.exists() {
|
&& !path.exists()
|
||||||
|
{
|
||||||
bail!("SSH private key does not exist: {:?}", path);
|
bail!("SSH private key does not exist: {:?}", path);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(ref path) = self.public_key_path {
|
if let Some(ref path) = self.public_key_path
|
||||||
if !path.exists() {
|
&& !path.exists()
|
||||||
|
{
|
||||||
bail!("SSH public key does not exist: {:?}", path);
|
bail!("SSH public key does not exist: {:?}", path);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -336,7 +357,15 @@ impl SshConfig {
|
|||||||
if let Some(ref cmd) = self.ssh_command {
|
if let Some(ref cmd) = self.ssh_command {
|
||||||
Some(cmd.clone())
|
Some(cmd.clone())
|
||||||
} else if let Some(ref key_path) = self.private_key_path {
|
} else if let Some(ref key_path) = self.private_key_path {
|
||||||
Some(format!("ssh -i '{}'", key_path.display()))
|
let path_str = key_path.display().to_string();
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
Some(format!("ssh -i \"{}\"", path_str.replace('\\', "/")))
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
{
|
||||||
|
Some(format!("ssh -i '{}'", path_str))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -382,10 +411,6 @@ impl GpgConfig {
|
|||||||
/// Token configuration for services (GitHub, GitLab, etc.)
|
/// Token configuration for services (GitHub, GitLab, etc.)
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct TokenConfig {
|
pub struct TokenConfig {
|
||||||
/// Token value (encrypted)
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub token: Option<String>,
|
|
||||||
|
|
||||||
/// Token type (personal, oauth, etc.)
|
/// Token type (personal, oauth, etc.)
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub token_type: TokenType,
|
pub token_type: TokenType,
|
||||||
@@ -405,25 +430,41 @@ pub struct TokenConfig {
|
|||||||
/// Description
|
/// Description
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
|
/// Indicates if a token is stored in keyring
|
||||||
|
#[serde(default)]
|
||||||
|
pub has_token: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenConfig {
|
impl TokenConfig {
|
||||||
/// Create a new token config
|
/// Create a new token config (token stored separately in keyring)
|
||||||
pub fn new(token: String, token_type: TokenType) -> Self {
|
pub fn new(token_type: TokenType) -> Self {
|
||||||
Self {
|
Self {
|
||||||
token: Some(token),
|
|
||||||
token_type,
|
token_type,
|
||||||
scopes: vec![],
|
scopes: vec![],
|
||||||
expires_at: None,
|
expires_at: None,
|
||||||
last_used: None,
|
last_used: None,
|
||||||
description: None,
|
description: None,
|
||||||
|
has_token: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new token config without token
|
||||||
|
pub fn without_token(token_type: TokenType) -> Self {
|
||||||
|
Self {
|
||||||
|
token_type,
|
||||||
|
scopes: vec![],
|
||||||
|
expires_at: None,
|
||||||
|
last_used: None,
|
||||||
|
description: None,
|
||||||
|
has_token: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate token configuration
|
/// Validate token configuration
|
||||||
pub fn validate(&self) -> Result<()> {
|
pub fn validate(&self) -> Result<()> {
|
||||||
if self.token.is_none() && self.token_type != TokenType::None {
|
if !self.has_token && self.token_type != TokenType::None {
|
||||||
bail!("Token value is required for {:?}", self.token_type);
|
bail!("Token is required for {:?}", self.token_type);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -432,12 +473,19 @@ impl TokenConfig {
|
|||||||
pub fn record_usage(&mut self) {
|
pub fn record_usage(&mut self) {
|
||||||
self.last_used = Some(chrono::Utc::now().to_rfc3339());
|
self.last_used = Some(chrono::Utc::now().to_rfc3339());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mark that a token is stored
|
||||||
|
pub fn set_has_token(&mut self, has_token: bool) {
|
||||||
|
self.has_token = has_token;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Token type
|
/// Token type
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
|
#[derive(Default)]
|
||||||
pub enum TokenType {
|
pub enum TokenType {
|
||||||
|
#[default]
|
||||||
None,
|
None,
|
||||||
Personal,
|
Personal,
|
||||||
OAuth,
|
OAuth,
|
||||||
@@ -445,12 +493,6 @@ pub enum TokenType {
|
|||||||
App,
|
App,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TokenType {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for TokenType {
|
impl std::fmt::Display for TokenType {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
@@ -496,7 +538,11 @@ pub struct ConfigDifference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn default_gpg_program() -> String {
|
fn default_gpg_program() -> String {
|
||||||
|
if cfg!(target_os = "windows") {
|
||||||
|
"gpg.exe".to_string()
|
||||||
|
} else {
|
||||||
"gpg".to_string()
|
"gpg".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_true() -> bool {
|
fn default_true() -> bool {
|
||||||
@@ -576,9 +622,15 @@ impl GitProfileBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<GitProfile> {
|
pub fn build(self) -> Result<GitProfile> {
|
||||||
let name = self.name.ok_or_else(|| anyhow::anyhow!("Name is required"))?;
|
let name = self
|
||||||
let user_name = self.user_name.ok_or_else(|| anyhow::anyhow!("User name is required"))?;
|
.name
|
||||||
let user_email = self.user_email.ok_or_else(|| anyhow::anyhow!("User email is required"))?;
|
.ok_or_else(|| anyhow::anyhow!("Name is required"))?;
|
||||||
|
let user_name = self
|
||||||
|
.user_name
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("User name is required"))?;
|
||||||
|
let user_email = self
|
||||||
|
.user_email
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("User email is required"))?;
|
||||||
|
|
||||||
Ok(GitProfile {
|
Ok(GitProfile {
|
||||||
name,
|
name,
|
||||||
@@ -630,7 +682,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_token_config() {
|
fn test_token_config() {
|
||||||
let token = TokenConfig::new("test-token".to_string(), TokenType::Personal);
|
let token = TokenConfig::new(TokenType::Personal);
|
||||||
assert!(token.validate().is_ok());
|
assert!(token.validate().is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use crate::config::{CommitFormat, LlmConfig};
|
use crate::config::manager::ConfigManager;
|
||||||
|
use crate::config::{CommitFormat, Language};
|
||||||
use crate::git::{CommitInfo, GitRepo};
|
use crate::git::{CommitInfo, GitRepo};
|
||||||
use crate::llm::{GeneratedCommit, LlmClient};
|
use crate::llm::{GeneratedCommit, LlmClient};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use chrono::Utc;
|
|
||||||
|
|
||||||
/// Content generator using LLM
|
/// Content generator using LLM
|
||||||
pub struct ContentGenerator {
|
pub struct ContentGenerator {
|
||||||
@@ -11,32 +11,63 @@ pub struct ContentGenerator {
|
|||||||
|
|
||||||
impl ContentGenerator {
|
impl ContentGenerator {
|
||||||
/// Create new content generator
|
/// Create new content generator
|
||||||
pub async fn new(config: &LlmConfig) -> Result<Self> {
|
pub async fn new(manager: &ConfigManager) -> Result<Self> {
|
||||||
let llm_client = LlmClient::from_config(config).await?;
|
Self::new_with_think(manager, false).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create new content generator with thinking override
|
||||||
|
pub async fn new_with_think(manager: &ConfigManager, think_override: bool) -> Result<Self> {
|
||||||
|
let mut thinking_enabled = if think_override {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
manager.config().llm.thinking_enabled
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validate thinking support per provider
|
||||||
|
if thinking_enabled {
|
||||||
|
let provider = manager.llm_provider();
|
||||||
|
if !Self::supports_thinking(provider) {
|
||||||
|
eprintln!(
|
||||||
|
"Warning: Provider '{}' does not support thinking mode. \
|
||||||
|
Disabling thinking for this invocation.",
|
||||||
|
provider
|
||||||
|
);
|
||||||
|
thinking_enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let llm_client = LlmClient::from_config_with_think(manager, thinking_enabled).await?;
|
||||||
|
|
||||||
// Check if provider is available
|
|
||||||
if !llm_client.is_available().await {
|
if !llm_client.is_available().await {
|
||||||
anyhow::bail!("LLM provider '{}' is not available", config.provider);
|
anyhow::bail!("LLM provider '{}' is not available", manager.llm_provider());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self { llm_client })
|
Ok(Self { llm_client })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn supports_thinking(provider: &str) -> bool {
|
||||||
|
matches!(provider, "deepseek" | "kimi" | "anthropic" | "openai")
|
||||||
|
}
|
||||||
|
|
||||||
/// Generate commit message from diff
|
/// Generate commit message from diff
|
||||||
pub async fn generate_commit_message(
|
pub async fn generate_commit_message(
|
||||||
&self,
|
&self,
|
||||||
diff: &str,
|
diff: &str,
|
||||||
format: CommitFormat,
|
format: CommitFormat,
|
||||||
|
language: Language,
|
||||||
) -> Result<GeneratedCommit> {
|
) -> Result<GeneratedCommit> {
|
||||||
// Truncate diff if too long
|
// Truncate diff if too long
|
||||||
let max_diff_len = 4000;
|
let max_diff_len = 4000;
|
||||||
let truncated_diff = if diff.len() > max_diff_len {
|
let truncated_diff = if diff.len() > max_diff_len {
|
||||||
format!("{}\n... (truncated)", &diff[..max_diff_len])
|
let boundary = diff.floor_char_boundary(max_diff_len);
|
||||||
|
format!("{}\n... (truncated)", &diff[..boundary])
|
||||||
} else {
|
} else {
|
||||||
diff.to_string()
|
diff.to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
self.llm_client.generate_commit_message(&truncated_diff, format).await
|
self.llm_client
|
||||||
|
.generate_commit_message(&truncated_diff, format, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate commit message from repository changes
|
/// Generate commit message from repository changes
|
||||||
@@ -44,15 +75,17 @@ impl ContentGenerator {
|
|||||||
&self,
|
&self,
|
||||||
repo: &GitRepo,
|
repo: &GitRepo,
|
||||||
format: CommitFormat,
|
format: CommitFormat,
|
||||||
|
language: Language,
|
||||||
) -> Result<GeneratedCommit> {
|
) -> Result<GeneratedCommit> {
|
||||||
let diff = repo.get_staged_diff()
|
let diff = repo
|
||||||
|
.get_staged_diff_sorted()
|
||||||
.context("Failed to get staged diff")?;
|
.context("Failed to get staged diff")?;
|
||||||
|
|
||||||
if diff.is_empty() {
|
if diff.is_empty() {
|
||||||
anyhow::bail!("No staged changes to generate commit from");
|
anyhow::bail!("No staged changes to generate commit from");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.generate_commit_message(&diff, format).await
|
self.generate_commit_message(&diff, format, language).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate tag message
|
/// Generate tag message
|
||||||
@@ -60,13 +93,14 @@ impl ContentGenerator {
|
|||||||
&self,
|
&self,
|
||||||
version: &str,
|
version: &str,
|
||||||
commits: &[CommitInfo],
|
commits: &[CommitInfo],
|
||||||
|
language: Language,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let commit_messages: Vec<String> = commits
|
let commit_messages: Vec<String> =
|
||||||
.iter()
|
commits.iter().map(|c| c.subject().to_string()).collect();
|
||||||
.map(|c| c.subject().to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
self.llm_client.generate_tag_message(version, &commit_messages).await
|
self.llm_client
|
||||||
|
.generate_tag_message(version, &commit_messages, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate changelog entry
|
/// Generate changelog entry
|
||||||
@@ -74,6 +108,7 @@ impl ContentGenerator {
|
|||||||
&self,
|
&self,
|
||||||
version: &str,
|
version: &str,
|
||||||
commits: &[CommitInfo],
|
commits: &[CommitInfo],
|
||||||
|
language: Language,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let typed_commits: Vec<(String, String)> = commits
|
let typed_commits: Vec<(String, String)> = commits
|
||||||
.iter()
|
.iter()
|
||||||
@@ -83,7 +118,9 @@ impl ContentGenerator {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
self.llm_client.generate_changelog_entry(version, &typed_commits).await
|
self.llm_client
|
||||||
|
.generate_changelog_entry(version, &typed_commits, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate changelog from repository
|
/// Generate changelog from repository
|
||||||
@@ -92,6 +129,7 @@ impl ContentGenerator {
|
|||||||
repo: &GitRepo,
|
repo: &GitRepo,
|
||||||
version: &str,
|
version: &str,
|
||||||
from_tag: Option<&str>,
|
from_tag: Option<&str>,
|
||||||
|
language: Language,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let commits = if let Some(tag) = from_tag {
|
let commits = if let Some(tag) = from_tag {
|
||||||
repo.get_commits_between(tag, "HEAD")?
|
repo.get_commits_between(tag, "HEAD")?
|
||||||
@@ -99,7 +137,8 @@ impl ContentGenerator {
|
|||||||
repo.get_commits(50)?
|
repo.get_commits(50)?
|
||||||
};
|
};
|
||||||
|
|
||||||
self.generate_changelog_entry(version, &commits).await
|
self.generate_changelog_entry(version, &commits, language)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interactive commit generation with user feedback
|
/// Interactive commit generation with user feedback
|
||||||
@@ -107,11 +146,11 @@ impl ContentGenerator {
|
|||||||
&self,
|
&self,
|
||||||
repo: &GitRepo,
|
repo: &GitRepo,
|
||||||
format: CommitFormat,
|
format: CommitFormat,
|
||||||
|
language: Language,
|
||||||
) -> Result<GeneratedCommit> {
|
) -> Result<GeneratedCommit> {
|
||||||
use dialoguer::{Confirm, Select};
|
use dialoguer::Select;
|
||||||
use console::Term;
|
|
||||||
|
|
||||||
let diff = repo.get_staged_diff()?;
|
let diff = repo.get_staged_diff_sorted()?;
|
||||||
|
|
||||||
if diff.is_empty() {
|
if diff.is_empty() {
|
||||||
anyhow::bail!("No staged changes");
|
anyhow::bail!("No staged changes");
|
||||||
@@ -126,7 +165,9 @@ impl ContentGenerator {
|
|||||||
|
|
||||||
// Generate initial commit
|
// Generate initial commit
|
||||||
println!("\nGenerating commit message...");
|
println!("\nGenerating commit message...");
|
||||||
let mut generated = self.generate_commit_message(&diff, format).await?;
|
let mut generated = self
|
||||||
|
.generate_commit_message(&diff, format, language)
|
||||||
|
.await?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
println!("\n{}", "─".repeat(60));
|
println!("\n{}", "─".repeat(60));
|
||||||
@@ -139,7 +180,6 @@ impl ContentGenerator {
|
|||||||
"✓ Accept and commit",
|
"✓ Accept and commit",
|
||||||
"🔄 Regenerate",
|
"🔄 Regenerate",
|
||||||
"✏️ Edit",
|
"✏️ Edit",
|
||||||
"📋 Copy to clipboard",
|
|
||||||
"❌ Cancel",
|
"❌ Cancel",
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -153,30 +193,21 @@ impl ContentGenerator {
|
|||||||
0 => return Ok(generated),
|
0 => return Ok(generated),
|
||||||
1 => {
|
1 => {
|
||||||
println!("Regenerating...");
|
println!("Regenerating...");
|
||||||
generated = self.generate_commit_message(&diff, format).await?;
|
generated = self
|
||||||
|
.generate_commit_message(&diff, format, language)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
2 => {
|
2 => {
|
||||||
let edited = crate::utils::editor::edit_content(&generated.to_conventional())?;
|
let edited = crate::utils::editor::edit_content(&generated.to_conventional())?;
|
||||||
generated = self.parse_edited_commit(&edited, format)?;
|
generated = self.parse_edited_commit(&edited, format)?;
|
||||||
}
|
}
|
||||||
3 => {
|
3 => anyhow::bail!("Cancelled by user"),
|
||||||
#[cfg(feature = "clipboard")]
|
|
||||||
{
|
|
||||||
arboard::Clipboard::new()?.set_text(generated.to_conventional())?;
|
|
||||||
println!("Copied to clipboard!");
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "clipboard"))]
|
|
||||||
{
|
|
||||||
println!("Clipboard feature not enabled");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
4 => anyhow::bail!("Cancelled by user"),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_edited_commit(&self, edited: &str, format: CommitFormat) -> Result<GeneratedCommit> {
|
fn parse_edited_commit(&self, edited: &str, _format: CommitFormat) -> Result<GeneratedCommit> {
|
||||||
let parsed = crate::git::commit::parse_commit_message(edited);
|
let parsed = crate::git::commit::parse_commit_message(edited);
|
||||||
|
|
||||||
Ok(GeneratedCommit {
|
Ok(GeneratedCommit {
|
||||||
@@ -190,115 +221,8 @@ impl ContentGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Batch generator for multiple operations
|
|
||||||
pub struct BatchGenerator {
|
|
||||||
generator: ContentGenerator,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BatchGenerator {
|
|
||||||
/// Create new batch generator
|
|
||||||
pub async fn new(config: &LlmConfig) -> Result<Self> {
|
|
||||||
let generator = ContentGenerator::new(config).await?;
|
|
||||||
Ok(Self { generator })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate commits for multiple repositories
|
|
||||||
pub async fn generate_commits_batch<'a>(
|
|
||||||
&self,
|
|
||||||
repos: &[&'a GitRepo],
|
|
||||||
format: CommitFormat,
|
|
||||||
) -> Vec<(&'a str, Result<GeneratedCommit>)> {
|
|
||||||
let mut results = vec![];
|
|
||||||
|
|
||||||
for repo in repos {
|
|
||||||
let result = self.generator.generate_commit_from_repo(repo, format).await;
|
|
||||||
results.push((repo.path().to_str().unwrap_or("unknown"), result));
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate changelog for multiple versions
|
|
||||||
pub async fn generate_changelog_batch(
|
|
||||||
&self,
|
|
||||||
repo: &GitRepo,
|
|
||||||
versions: &[String],
|
|
||||||
) -> Vec<(String, Result<String>)> {
|
|
||||||
let mut results = vec![];
|
|
||||||
|
|
||||||
// Get all tags
|
|
||||||
let tags = repo.get_tags().unwrap_or_default();
|
|
||||||
|
|
||||||
for (i, version) in versions.iter().enumerate() {
|
|
||||||
let from_tag = if i + 1 < tags.len() {
|
|
||||||
tags.get(i + 1).map(|t| t.name.as_str())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = self.generator.generate_changelog_from_repo(repo, version, from_tag).await;
|
|
||||||
results.push((version.clone(), result));
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generator options
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct GeneratorOptions {
|
|
||||||
pub auto_commit: bool,
|
|
||||||
pub auto_push: bool,
|
|
||||||
pub interactive: bool,
|
|
||||||
pub dry_run: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for GeneratorOptions {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
auto_commit: false,
|
|
||||||
auto_push: false,
|
|
||||||
interactive: true,
|
|
||||||
dry_run: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate with options
|
|
||||||
pub async fn generate_with_options(
|
|
||||||
repo: &GitRepo,
|
|
||||||
config: &LlmConfig,
|
|
||||||
format: CommitFormat,
|
|
||||||
options: GeneratorOptions,
|
|
||||||
) -> Result<Option<GeneratedCommit>> {
|
|
||||||
let generator = ContentGenerator::new(config).await?;
|
|
||||||
|
|
||||||
let generated = if options.interactive {
|
|
||||||
generator.generate_commit_interactive(repo, format).await?
|
|
||||||
} else {
|
|
||||||
generator.generate_commit_from_repo(repo, format).await?
|
|
||||||
};
|
|
||||||
|
|
||||||
if options.dry_run {
|
|
||||||
println!("{}", generated.to_conventional());
|
|
||||||
return Ok(Some(generated));
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.auto_commit {
|
|
||||||
let message = generated.to_conventional();
|
|
||||||
repo.commit(&message, false)?;
|
|
||||||
|
|
||||||
if options.auto_push {
|
|
||||||
repo.push("origin", "HEAD")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Some(generated))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fallback generators when LLM is not available
|
/// Fallback generators when LLM is not available
|
||||||
pub mod fallback {
|
pub mod fallback {
|
||||||
use super::*;
|
|
||||||
use crate::git::commit::create_date_commit_message;
|
use crate::git::commit::create_date_commit_message;
|
||||||
|
|
||||||
/// Generate simple commit message without LLM
|
/// Generate simple commit message without LLM
|
||||||
@@ -323,9 +247,13 @@ pub mod fallback {
|
|||||||
f.ends_with(".rs") || f.ends_with(".py") || f.ends_with(".js") || f.ends_with(".ts")
|
f.ends_with(".rs") || f.ends_with(".py") || f.ends_with(".js") || f.ends_with(".ts")
|
||||||
});
|
});
|
||||||
|
|
||||||
let has_docs = files.iter().any(|f| f.ends_with(".md") || f.contains("README"));
|
let has_docs = files
|
||||||
|
.iter()
|
||||||
|
.any(|f| f.ends_with(".md") || f.contains("README"));
|
||||||
|
|
||||||
let has_tests = files.iter().any(|f| f.contains("test") || f.contains("spec"));
|
let has_tests = files
|
||||||
|
.iter()
|
||||||
|
.any(|f| f.contains("test") || f.contains("spec"));
|
||||||
|
|
||||||
if has_tests {
|
if has_tests {
|
||||||
"test: update tests".to_string()
|
"test: update tests".to_string()
|
||||||
|
|||||||
@@ -1,10 +1,19 @@
|
|||||||
use super::{CommitInfo, GitRepo};
|
use super::{CommitInfo, GitRepo};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use chrono::{DateTime, TimeZone, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
pub const CHANGELOG_HEADER: &str = r#"# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
"#;
|
||||||
|
|
||||||
/// Changelog generator
|
/// Changelog generator
|
||||||
pub struct ChangelogGenerator {
|
pub struct ChangelogGenerator {
|
||||||
format: ChangelogFormat,
|
format: ChangelogFormat,
|
||||||
@@ -86,9 +95,7 @@ impl ChangelogGenerator {
|
|||||||
ChangelogFormat::GitHubReleases => {
|
ChangelogFormat::GitHubReleases => {
|
||||||
self.generate_github_releases(version, date, commits)
|
self.generate_github_releases(version, date, commits)
|
||||||
}
|
}
|
||||||
ChangelogFormat::Custom => {
|
ChangelogFormat::Custom => self.generate_custom(version, date, commits),
|
||||||
self.generate_custom(version, date, commits)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,9 +116,10 @@ impl ChangelogGenerator {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let new_content = if existing.is_empty() {
|
let new_content = if existing.is_empty() {
|
||||||
format!("# Changelog\n\n{}", entry)
|
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||||
} else {
|
} else if existing.starts_with(CHANGELOG_HEADER) {
|
||||||
// Find position after header
|
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||||
|
} else if existing.starts_with("# Changelog") {
|
||||||
let lines: Vec<&str> = existing.lines().collect();
|
let lines: Vec<&str> = existing.lines().collect();
|
||||||
let mut header_end = 0;
|
let mut header_end = 0;
|
||||||
|
|
||||||
@@ -129,6 +137,8 @@ impl ChangelogGenerator {
|
|||||||
let rest = lines[header_end..].join("\n");
|
let rest = lines[header_end..].join("\n");
|
||||||
|
|
||||||
format!("{}\n{}\n{}", header, entry, rest)
|
format!("{}\n{}\n{}", header, entry, rest)
|
||||||
|
} else {
|
||||||
|
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||||
};
|
};
|
||||||
|
|
||||||
fs::write(changelog_path, new_content)
|
fs::write(changelog_path, new_content)
|
||||||
@@ -147,7 +157,7 @@ impl ChangelogGenerator {
|
|||||||
let mut output = format!("## [{}] - {}\n\n", version, date_str);
|
let mut output = format!("## [{}] - {}\n\n", version, date_str);
|
||||||
|
|
||||||
if self.group_by_type {
|
if self.group_by_type {
|
||||||
let grouped = self.group_commits(commits);
|
let _grouped = self.group_commits(commits);
|
||||||
|
|
||||||
// Standard categories
|
// Standard categories
|
||||||
let categories = vec![
|
let categories = vec![
|
||||||
@@ -218,11 +228,11 @@ impl ChangelogGenerator {
|
|||||||
|
|
||||||
fn generate_github_releases(
|
fn generate_github_releases(
|
||||||
&self,
|
&self,
|
||||||
version: &str,
|
_version: &str,
|
||||||
_date: DateTime<Utc>,
|
_date: DateTime<Utc>,
|
||||||
commits: &[CommitInfo],
|
commits: &[CommitInfo],
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let mut output = format!("## What's Changed\n\n");
|
let mut output = "## What's Changed\n\n".to_string();
|
||||||
|
|
||||||
// Group by type
|
// Group by type
|
||||||
let mut features = vec![];
|
let mut features = vec![];
|
||||||
@@ -232,8 +242,6 @@ impl ChangelogGenerator {
|
|||||||
let mut breaking = vec![];
|
let mut breaking = vec![];
|
||||||
|
|
||||||
for commit in commits {
|
for commit in commits {
|
||||||
let msg = commit.subject();
|
|
||||||
|
|
||||||
if commit.message.contains("BREAKING CHANGE") {
|
if commit.message.contains("BREAKING CHANGE") {
|
||||||
breaking.push(commit);
|
breaking.push(commit);
|
||||||
}
|
}
|
||||||
@@ -347,7 +355,12 @@ impl ChangelogGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn format_commit_github(&self, commit: &CommitInfo) -> String {
|
fn format_commit_github(&self, commit: &CommitInfo) -> String {
|
||||||
format!("- {} by @{} in {}\n", commit.subject(), commit.author, &commit.short_id)
|
format!(
|
||||||
|
"- {} by @{} in {}\n",
|
||||||
|
commit.subject(),
|
||||||
|
commit.author,
|
||||||
|
&commit.short_id
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn group_commits<'a>(&self, commits: &'a [CommitInfo]) -> HashMap<String, Vec<&'a CommitInfo>> {
|
fn group_commits<'a>(&self, commits: &'a [CommitInfo]) -> HashMap<String, Vec<&'a CommitInfo>> {
|
||||||
@@ -370,8 +383,7 @@ impl Default for ChangelogGenerator {
|
|||||||
|
|
||||||
/// Read existing changelog
|
/// Read existing changelog
|
||||||
pub fn read_changelog(path: &Path) -> Result<String> {
|
pub fn read_changelog(path: &Path) -> Result<String> {
|
||||||
fs::read_to_string(path)
|
fs::read_to_string(path).with_context(|| format!("Failed to read changelog: {:?}", path))
|
||||||
.with_context(|| format!("Failed to read changelog: {:?}", path))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize new changelog file
|
/// Initialize new changelog file
|
||||||
@@ -380,16 +392,7 @@ pub fn init_changelog(path: &Path) -> Result<()> {
|
|||||||
anyhow::bail!("Changelog already exists at {:?}", path);
|
anyhow::bail!("Changelog already exists at {:?}", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = r#"# Changelog
|
fs::write(path, CHANGELOG_HEADER)
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
|
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
||||||
|
|
||||||
"#;
|
|
||||||
|
|
||||||
fs::write(path, content)
|
|
||||||
.with_context(|| format!("Failed to create changelog: {:?}", path))?;
|
.with_context(|| format!("Failed to create changelog: {:?}", path))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -412,11 +415,7 @@ pub fn generate_from_history(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update version links in changelog
|
/// Update version links in changelog
|
||||||
pub fn update_version_links(
|
pub fn update_version_links(changelog: &str, version: &str, compare_url: &str) -> String {
|
||||||
changelog: &str,
|
|
||||||
version: &str,
|
|
||||||
compare_url: &str,
|
|
||||||
) -> String {
|
|
||||||
// Add version link at the end of changelog
|
// Add version link at the end of changelog
|
||||||
format!("{}\n[{}]: {}\n", changelog, version, compare_url)
|
format!("{}\n[{}]: {}\n", changelog, version, compare_url)
|
||||||
}
|
}
|
||||||
@@ -426,20 +425,19 @@ pub fn parse_versions(changelog: &str) -> Vec<(String, String)> {
|
|||||||
let mut versions = vec![];
|
let mut versions = vec![];
|
||||||
|
|
||||||
for line in changelog.lines() {
|
for line in changelog.lines() {
|
||||||
if line.starts_with("## [") {
|
if line.starts_with("## [")
|
||||||
if let Some(start) = line.find('[') {
|
&& let Some(start) = line.find('[')
|
||||||
if let Some(end) = line.find(']') {
|
&& let Some(end) = line.find(']')
|
||||||
|
{
|
||||||
let version = &line[start + 1..end];
|
let version = &line[start + 1..end];
|
||||||
if version != "Unreleased" {
|
if version != "Unreleased"
|
||||||
if let Some(date_start) = line.find(" - ") {
|
&& let Some(date_start) = line.find(" - ")
|
||||||
|
{
|
||||||
let date = &line[date_start + 3..].trim();
|
let date = &line[date_start + 3..].trim();
|
||||||
versions.push((version.to_string(), date.to_string()));
|
versions.push((version.to_string(), date.to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
versions
|
versions
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::GitRepo;
|
use super::GitRepo;
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Result, bail};
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
|
||||||
/// Commit builder for creating commits
|
/// Commit builder for creating commits
|
||||||
@@ -9,11 +9,11 @@ pub struct CommitBuilder {
|
|||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
body: Option<String>,
|
body: Option<String>,
|
||||||
footer: Option<String>,
|
footer: Option<String>,
|
||||||
|
message: Option<String>,
|
||||||
breaking: bool,
|
breaking: bool,
|
||||||
sign: bool,
|
sign: bool,
|
||||||
amend: bool,
|
amend: bool,
|
||||||
no_verify: bool,
|
no_verify: bool,
|
||||||
dry_run: bool,
|
|
||||||
format: crate::config::CommitFormat,
|
format: crate::config::CommitFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,11 +26,11 @@ impl CommitBuilder {
|
|||||||
description: None,
|
description: None,
|
||||||
body: None,
|
body: None,
|
||||||
footer: None,
|
footer: None,
|
||||||
|
message: None,
|
||||||
breaking: false,
|
breaking: false,
|
||||||
sign: false,
|
sign: false,
|
||||||
amend: false,
|
amend: false,
|
||||||
no_verify: false,
|
no_verify: false,
|
||||||
dry_run: false,
|
|
||||||
format: crate::config::CommitFormat::Conventional,
|
format: crate::config::CommitFormat::Conventional,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,6 +47,12 @@ impl CommitBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set scope (optional)
|
||||||
|
pub fn scope_opt(mut self, scope: Option<String>) -> Self {
|
||||||
|
self.scope = scope;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set description
|
/// Set description
|
||||||
pub fn description(mut self, description: impl Into<String>) -> Self {
|
pub fn description(mut self, description: impl Into<String>) -> Self {
|
||||||
self.description = Some(description.into());
|
self.description = Some(description.into());
|
||||||
@@ -59,12 +65,24 @@ impl CommitBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set body (optional)
|
||||||
|
pub fn body_opt(mut self, body: Option<String>) -> Self {
|
||||||
|
self.body = body;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set footer
|
/// Set footer
|
||||||
pub fn footer(mut self, footer: impl Into<String>) -> Self {
|
pub fn footer(mut self, footer: impl Into<String>) -> Self {
|
||||||
self.footer = Some(footer.into());
|
self.footer = Some(footer.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set message
|
||||||
|
pub fn message(mut self, message: impl Into<String>) -> Self {
|
||||||
|
self.message = Some(message.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Mark as breaking change
|
/// Mark as breaking change
|
||||||
pub fn breaking(mut self, breaking: bool) -> Self {
|
pub fn breaking(mut self, breaking: bool) -> Self {
|
||||||
self.breaking = breaking;
|
self.breaking = breaking;
|
||||||
@@ -89,12 +107,6 @@ impl CommitBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dry run (don't actually commit)
|
|
||||||
pub fn dry_run(mut self, dry_run: bool) -> Self {
|
|
||||||
self.dry_run = dry_run;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set commit format
|
/// Set commit format
|
||||||
pub fn format(mut self, format: crate::config::CommitFormat) -> Self {
|
pub fn format(mut self, format: crate::config::CommitFormat) -> Self {
|
||||||
self.format = format;
|
self.format = format;
|
||||||
@@ -103,10 +115,18 @@ impl CommitBuilder {
|
|||||||
|
|
||||||
/// Build commit message
|
/// Build commit message
|
||||||
pub fn build_message(&self) -> Result<String> {
|
pub fn build_message(&self) -> Result<String> {
|
||||||
let commit_type = self.commit_type.as_ref()
|
if let Some(ref msg) = self.message {
|
||||||
|
return Ok(msg.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let commit_type = self
|
||||||
|
.commit_type
|
||||||
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Commit type is required"))?;
|
.ok_or_else(|| anyhow::anyhow!("Commit type is required"))?;
|
||||||
|
|
||||||
let description = self.description.as_ref()
|
let description = self
|
||||||
|
.description
|
||||||
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Description is required"))?;
|
.ok_or_else(|| anyhow::anyhow!("Description is required"))?;
|
||||||
|
|
||||||
let message = match self.format {
|
let message = match self.format {
|
||||||
@@ -139,34 +159,14 @@ impl CommitBuilder {
|
|||||||
pub fn execute(&self, repo: &GitRepo) -> Result<Option<String>> {
|
pub fn execute(&self, repo: &GitRepo) -> Result<Option<String>> {
|
||||||
let message = self.build_message()?;
|
let message = self.build_message()?;
|
||||||
|
|
||||||
if self.dry_run {
|
|
||||||
return Ok(Some(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if there are staged changes
|
|
||||||
let staged_files = repo.get_staged_files()?;
|
|
||||||
if staged_files.is_empty() && !self.amend {
|
|
||||||
bail!("No staged changes to commit. Use 'git add' to stage files first.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate message
|
|
||||||
match self.format {
|
|
||||||
crate::config::CommitFormat::Conventional => {
|
|
||||||
crate::utils::validators::validate_conventional_commit(&message)?;
|
|
||||||
}
|
|
||||||
crate::config::CommitFormat::Commitlint => {
|
|
||||||
crate::utils::validators::validate_commitlint_commit(&message)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.amend {
|
if self.amend {
|
||||||
self.amend_commit(repo, &message)?;
|
self.amend_commit(repo, &message)?;
|
||||||
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
repo.commit(&message, self.sign)?;
|
repo.commit(&message, self.sign)?;
|
||||||
}
|
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn amend_commit(&self, repo: &GitRepo, message: &str) -> Result<()> {
|
fn amend_commit(&self, repo: &GitRepo, message: &str) -> Result<()> {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
@@ -190,8 +190,24 @@ impl CommitBuilder {
|
|||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
bail!("Failed to amend commit: {}", stderr);
|
|
||||||
|
let error_msg = if stderr.is_empty() {
|
||||||
|
if stdout.is_empty() {
|
||||||
|
"GPG signing failed. Please check:\n\
|
||||||
|
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||||
|
2. GPG agent is running\n\
|
||||||
|
3. You can sign commits manually (try: git commit --amend -S)"
|
||||||
|
.to_string()
|
||||||
|
} else {
|
||||||
|
stdout.to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stderr.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
bail!("Failed to amend commit: {}", error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -230,12 +246,19 @@ pub fn suggest_commit_type(diff: &str) -> Vec<&'static str> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for configuration files
|
// Check for configuration files
|
||||||
if diff.contains("config") || diff.contains(".json") || diff.contains(".yaml") || diff.contains(".toml") {
|
if diff.contains("config")
|
||||||
|
|| diff.contains(".json")
|
||||||
|
|| diff.contains(".yaml")
|
||||||
|
|| diff.contains(".toml")
|
||||||
|
{
|
||||||
suggestions.push("chore");
|
suggestions.push("chore");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for dependencies
|
// Check for dependencies
|
||||||
if diff.contains("Cargo.toml") || diff.contains("package.json") || diff.contains("requirements.txt") {
|
if diff.contains("Cargo.toml")
|
||||||
|
|| diff.contains("package.json")
|
||||||
|
|| diff.contains("requirements.txt")
|
||||||
|
{
|
||||||
suggestions.push("build");
|
suggestions.push("build");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,11 +315,12 @@ pub fn parse_commit_message(message: &str) -> ParsedCommit {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if line.starts_with("BREAKING CHANGE:") ||
|
if line.starts_with("BREAKING CHANGE:")
|
||||||
line.starts_with("Closes") ||
|
|| line.starts_with("Closes")
|
||||||
line.starts_with("Fixes") ||
|
|| line.starts_with("Fixes")
|
||||||
line.starts_with("Refs") ||
|
|| line.starts_with("Refs")
|
||||||
line.starts_with("Co-authored-by:") {
|
|| line.starts_with("Co-authored-by:")
|
||||||
|
{
|
||||||
in_footer = true;
|
in_footer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,8 +335,16 @@ pub fn parse_commit_message(message: &str) -> ParsedCommit {
|
|||||||
commit_type,
|
commit_type,
|
||||||
scope,
|
scope,
|
||||||
description: Some(description.to_string()),
|
description: Some(description.to_string()),
|
||||||
body: if body_lines.is_empty() { None } else { Some(body_lines.join("\n")) },
|
body: if body_lines.is_empty() {
|
||||||
footer: if footer_lines.is_empty() { None } else { Some(footer_lines.join("\n")) },
|
None
|
||||||
|
} else {
|
||||||
|
Some(body_lines.join("\n"))
|
||||||
|
},
|
||||||
|
footer: if footer_lines.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(footer_lines.join("\n"))
|
||||||
|
},
|
||||||
breaking,
|
breaking,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
720
src/git/mod.rs
720
src/git/mod.rs
@@ -1,17 +1,168 @@
|
|||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use git2::{Repository, Signature, StatusOptions, Config, Oid, ObjectType};
|
use git2::{Config, ObjectType, Oid, Repository, Signature, StatusOptions};
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::{Component, Path, PathBuf};
|
||||||
|
|
||||||
pub mod changelog;
|
pub mod changelog;
|
||||||
pub mod commit;
|
pub mod commit;
|
||||||
pub mod tag;
|
pub mod tag;
|
||||||
|
|
||||||
pub use changelog::ChangelogGenerator;
|
fn normalize_path_for_git2(path: &Path) -> PathBuf {
|
||||||
pub use commit::CommitBuilder;
|
let mut normalized = path.to_path_buf();
|
||||||
pub use tag::TagBuilder;
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
let path_str = path.to_string_lossy();
|
||||||
|
if path_str.starts_with(r"\\?\")
|
||||||
|
&& let Some(stripped) = path_str.strip_prefix(r"\\?\")
|
||||||
|
{
|
||||||
|
normalized = PathBuf::from(stripped);
|
||||||
|
}
|
||||||
|
if path_str.starts_with(r"\\?\UNC\")
|
||||||
|
&& let Some(stripped) = path_str.strip_prefix(r"\\?\UNC\")
|
||||||
|
{
|
||||||
|
normalized = PathBuf::from(format!(r"\\{}", stripped));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
normalized
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_absolute_path<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
|
||||||
|
if path.is_absolute() {
|
||||||
|
return Ok(normalize_path_for_git2(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
let current_dir = std::env::current_dir().with_context(|| "Failed to get current directory")?;
|
||||||
|
|
||||||
|
let absolute = current_dir.join(path);
|
||||||
|
Ok(normalize_path_for_git2(&absolute))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_path_without_canonicalize(path: &Path) -> PathBuf {
|
||||||
|
let mut components = Vec::new();
|
||||||
|
|
||||||
|
for component in path.components() {
|
||||||
|
match component {
|
||||||
|
Component::ParentDir => {
|
||||||
|
if !components.is_empty() && components.last() != Some(&Component::ParentDir) {
|
||||||
|
components.pop();
|
||||||
|
} else {
|
||||||
|
components.push(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Component::CurDir => {}
|
||||||
|
_ => components.push(component),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = PathBuf::new();
|
||||||
|
for component in components {
|
||||||
|
result.push(component.as_os_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
normalize_path_for_git2(&result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_open_repo_with_git2(path: &Path) -> Result<Repository> {
|
||||||
|
let normalized = normalize_path_for_git2(path);
|
||||||
|
|
||||||
|
let discover_opts = git2::RepositoryOpenFlags::empty();
|
||||||
|
let ceiling_dirs: [&str; 0] = [];
|
||||||
|
|
||||||
|
let repo = Repository::open_ext(&normalized, discover_opts, ceiling_dirs)
|
||||||
|
.or_else(|_| Repository::discover(&normalized))
|
||||||
|
.or_else(|_| Repository::open(&normalized));
|
||||||
|
|
||||||
|
repo.map_err(|e| anyhow::anyhow!("git2 failed: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_open_repo_with_git_cli(path: &Path) -> Result<Repository> {
|
||||||
|
let output = std::process::Command::new("git")
|
||||||
|
.args(["rev-parse", "--show-toplevel"])
|
||||||
|
.current_dir(path)
|
||||||
|
.output()
|
||||||
|
.context("Failed to execute git command")?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
bail!("git CLI failed to find repository");
|
||||||
|
}
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let git_root = stdout.trim();
|
||||||
|
|
||||||
|
if git_root.is_empty() {
|
||||||
|
bail!("git CLI returned empty path");
|
||||||
|
}
|
||||||
|
|
||||||
|
let git_root_path = PathBuf::from(git_root);
|
||||||
|
let normalized = normalize_path_for_git2(&git_root_path);
|
||||||
|
|
||||||
|
Repository::open(&normalized)
|
||||||
|
.with_context(|| format!("Failed to open repo from git CLI path: {:?}", normalized))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn diagnose_repo_issue(path: &Path) -> String {
|
||||||
|
let mut issues = Vec::new();
|
||||||
|
|
||||||
|
if !path.exists() {
|
||||||
|
issues.push(format!("Path does not exist: {:?}", path));
|
||||||
|
} else if !path.is_dir() {
|
||||||
|
issues.push(format!("Path is not a directory: {:?}", path));
|
||||||
|
}
|
||||||
|
|
||||||
|
let git_dir = path.join(".git");
|
||||||
|
if git_dir.exists() {
|
||||||
|
if git_dir.is_dir() {
|
||||||
|
issues.push("Found .git directory".to_string());
|
||||||
|
let config_file = git_dir.join("config");
|
||||||
|
if config_file.exists() {
|
||||||
|
issues.push("Git config file exists".to_string());
|
||||||
|
} else {
|
||||||
|
issues.push("WARNING: Git config file missing".to_string());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
issues.push("Found .git file (submodule or worktree)".to_string());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
issues.push("No .git found in current directory".to_string());
|
||||||
|
|
||||||
|
let mut current = path;
|
||||||
|
let mut depth = 0;
|
||||||
|
while let Some(parent) = current.parent() {
|
||||||
|
depth += 1;
|
||||||
|
if depth > 20 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let parent_git = parent.join(".git");
|
||||||
|
if parent_git.exists() {
|
||||||
|
issues.push(format!("Found .git in parent directory: {:?}", parent));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current = parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
let path_str = path.to_string_lossy();
|
||||||
|
if path_str.starts_with(r"\\?\") {
|
||||||
|
issues.push("Path has Windows extended-length prefix (\\\\?\\)".to_string());
|
||||||
|
}
|
||||||
|
if path_str.contains('\\') && path_str.contains('/') {
|
||||||
|
issues.push("WARNING: Path has mixed path separators".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(current_dir) = std::env::current_dir() {
|
||||||
|
issues.push(format!("Current working directory: {:?}", current_dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
issues.join("\n ")
|
||||||
|
}
|
||||||
|
|
||||||
/// Git repository wrapper with enhanced cross-platform support
|
|
||||||
pub struct GitRepo {
|
pub struct GitRepo {
|
||||||
repo: Repository,
|
repo: Repository,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
@@ -19,28 +170,46 @@ pub struct GitRepo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GitRepo {
|
impl GitRepo {
|
||||||
/// Open a git repository
|
|
||||||
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
|
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let absolute_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
|
|
||||||
|
|
||||||
let repo = Repository::discover(&absolute_path)
|
let absolute_path = get_absolute_path(path)?;
|
||||||
.or_else(|_| Repository::open(&absolute_path))
|
let resolved_path = resolve_path_without_canonicalize(&absolute_path);
|
||||||
.with_context(|| {
|
|
||||||
format!(
|
let repo = try_open_repo_with_git2(&resolved_path).or_else(|git2_err| {
|
||||||
"Failed to open git repository at '{:?}'. Please ensure:\n\
|
try_open_repo_with_git_cli(&resolved_path).map_err(|cli_err| {
|
||||||
1. The directory is set as safe (run: git config --global --add safe.directory \"{}\")\n\
|
let diagnosis = diagnose_repo_issue(&resolved_path);
|
||||||
2. The path is correct and contains a valid '.git' folder.",
|
anyhow::anyhow!(
|
||||||
absolute_path,
|
"Failed to open git repository:\n\
|
||||||
absolute_path.display()
|
\n\
|
||||||
|
=== git2 Error ===\n {}\n\
|
||||||
|
\n\
|
||||||
|
=== git CLI Error ===\n {}\n\
|
||||||
|
\n\
|
||||||
|
=== Diagnosis ===\n {}\n\
|
||||||
|
\n\
|
||||||
|
=== Suggestions ===\n\
|
||||||
|
1. Ensure you are inside a git repository\n\
|
||||||
|
2. Run: git status (to verify git works)\n\
|
||||||
|
3. Run: git config --global --add safe.directory \"*\"\n\
|
||||||
|
4. Check file permissions",
|
||||||
|
git2_err,
|
||||||
|
cli_err,
|
||||||
|
diagnosis
|
||||||
)
|
)
|
||||||
|
})
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
let repo_path = repo
|
||||||
|
.workdir()
|
||||||
|
.map(|p| p.to_path_buf())
|
||||||
|
.unwrap_or_else(|| resolved_path.clone());
|
||||||
|
|
||||||
let config = repo.config().ok();
|
let config = repo.config().ok();
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
repo,
|
repo,
|
||||||
path: absolute_path,
|
path: normalize_path_for_git2(&repo_path),
|
||||||
config,
|
config,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -78,7 +247,11 @@ impl GitRepo {
|
|||||||
pub fn get_user_name(&self) -> Result<String> {
|
pub fn get_user_name(&self) -> Result<String> {
|
||||||
self.get_config("user.name")?
|
self.get_config("user.name")?
|
||||||
.or_else(|| std::env::var("GIT_AUTHOR_NAME").ok())
|
.or_else(|| std::env::var("GIT_AUTHOR_NAME").ok())
|
||||||
.ok_or_else(|| anyhow::anyhow!("User name not configured. Set it with: git config user.name \"Your Name\""))
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"User name not configured. Set it with: git config user.name \"Your Name\""
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the configured user email
|
/// Get the configured user email
|
||||||
@@ -90,7 +263,8 @@ impl GitRepo {
|
|||||||
|
|
||||||
/// Get the configured GPG signing key
|
/// Get the configured GPG signing key
|
||||||
pub fn get_signing_key(&self) -> Result<Option<String>> {
|
pub fn get_signing_key(&self) -> Result<Option<String>> {
|
||||||
Ok(self.get_config("user.signingkey")?
|
Ok(self
|
||||||
|
.get_config("user.signingkey")?
|
||||||
.or_else(|| std::env::var("GIT_SIGNING_KEY").ok()))
|
.or_else(|| std::env::var("GIT_SIGNING_KEY").ok()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,23 +292,22 @@ impl GitRepo {
|
|||||||
return Ok(program);
|
return Ok(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_gpg = if cfg!(windows) {
|
let default_gpg = if cfg!(windows) { "gpg.exe" } else { "gpg" };
|
||||||
"gpg.exe"
|
|
||||||
} else {
|
|
||||||
"gpg"
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(default_gpg.to_string())
|
Ok(default_gpg.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a signature using repository configuration
|
/// Create a signature using repository configuration
|
||||||
pub fn create_signature(&self) -> Result<Signature> {
|
pub fn create_signature(&self) -> Result<Signature<'_>> {
|
||||||
let name = self.get_user_name()?;
|
let name = self.get_user_name()?;
|
||||||
let email = self.get_user_email()?;
|
let email = self.get_user_email()?;
|
||||||
let time = git2::Time::new(std::time::SystemTime::now()
|
let time = git2::Time::new(
|
||||||
|
std::time::SystemTime::now()
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_secs() as i64, 0);
|
.as_secs() as i64,
|
||||||
|
0,
|
||||||
|
);
|
||||||
Signature::new(&name, &email, &time).map_err(Into::into)
|
Signature::new(&name, &email, &time).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,31 +330,144 @@ impl GitRepo {
|
|||||||
|
|
||||||
/// Get staged diff
|
/// Get staged diff
|
||||||
pub fn get_staged_diff(&self) -> Result<String> {
|
pub fn get_staged_diff(&self) -> Result<String> {
|
||||||
let head = self.repo.head().ok();
|
// Use git CLI to get staged diff for better compatibility
|
||||||
let head_tree = head.as_ref()
|
let output = std::process::Command::new("git")
|
||||||
.and_then(|h| h.peel_to_tree().ok());
|
.args(["diff", "--cached"])
|
||||||
|
.current_dir(&self.path)
|
||||||
|
.output()
|
||||||
|
.with_context(|| "Failed to get staged diff with git command")?;
|
||||||
|
|
||||||
let mut index = self.repo.index()?;
|
if !output.status.success() {
|
||||||
let index_tree = index.write_tree()?;
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
let index_tree = self.repo.find_tree(index_tree)?;
|
bail!("Failed to get staged diff: {}", stderr);
|
||||||
|
|
||||||
let diff = if let Some(head) = head_tree {
|
|
||||||
self.repo.diff_tree_to_index(Some(&head), Some(&index), None)?
|
|
||||||
} else {
|
|
||||||
self.repo.diff_tree_to_index(None, Some(&index), None)?
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut diff_text = String::new();
|
|
||||||
diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
|
|
||||||
if let Ok(content) = std::str::from_utf8(line.content()) {
|
|
||||||
diff_text.push_str(content);
|
|
||||||
}
|
}
|
||||||
true
|
|
||||||
})?;
|
|
||||||
|
|
||||||
|
let diff_text = String::from_utf8_lossy(&output.stdout).to_string();
|
||||||
Ok(diff_text)
|
Ok(diff_text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get staged diff with files sorted by importance
|
||||||
|
/// Important files (source code) come first, then config files like Cargo.toml,
|
||||||
|
/// then lock files like Cargo.lock
|
||||||
|
pub fn get_staged_diff_sorted(&self) -> Result<String> {
|
||||||
|
let diff = self.get_staged_diff()?;
|
||||||
|
|
||||||
|
if diff.is_empty() {
|
||||||
|
return Ok(diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut file_diffs = Vec::new();
|
||||||
|
let mut current_file_diff = String::new();
|
||||||
|
let mut current_file = String::new();
|
||||||
|
|
||||||
|
for line in diff.lines() {
|
||||||
|
if line.starts_with("diff --git") {
|
||||||
|
// Save previous file diff if any
|
||||||
|
if !current_file_diff.is_empty() && !current_file.is_empty() {
|
||||||
|
file_diffs.push((current_file.clone(), current_file_diff.clone()));
|
||||||
|
}
|
||||||
|
current_file = extract_file_from_diff_line(line);
|
||||||
|
current_file_diff = format!("{}\n", line);
|
||||||
|
} else {
|
||||||
|
current_file_diff.push_str(line);
|
||||||
|
current_file_diff.push('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the last file diff
|
||||||
|
if !current_file_diff.is_empty() && !current_file.is_empty() {
|
||||||
|
file_diffs.push((current_file, current_file_diff));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by file importance
|
||||||
|
file_diffs.sort_by(|a, b| {
|
||||||
|
let score_a = file_importance_score(&a.0);
|
||||||
|
let score_b = file_importance_score(&b.0);
|
||||||
|
score_b.cmp(&score_a) // Descending order
|
||||||
|
});
|
||||||
|
|
||||||
|
// Combine sorted diffs
|
||||||
|
let sorted_diff: String = file_diffs.into_iter().map(|(_, diff)| diff).collect();
|
||||||
|
|
||||||
|
Ok(sorted_diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract filename from diff --git line
|
||||||
|
fn extract_file_from_diff_line(line: &str) -> String {
|
||||||
|
// Format: "diff --git a/path/to/file b/path/to/file"
|
||||||
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||||
|
if parts.len() >= 3 {
|
||||||
|
// Return the second path (after b/)
|
||||||
|
if let Some(path) = parts[2].strip_prefix("b/") {
|
||||||
|
return path.to_string();
|
||||||
|
}
|
||||||
|
// Fallback to first path (after a/)
|
||||||
|
if let Some(path) = parts[1].strip_prefix("a/") {
|
||||||
|
return path.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculate file importance score
|
||||||
|
/// Higher score = more important
|
||||||
|
fn file_importance_score(filename: &str) -> i32 {
|
||||||
|
// Priority list for important file types
|
||||||
|
let important_extensions = [
|
||||||
|
".rs", ".py", ".js", ".ts", ".tsx", ".jsx", ".go", ".java", ".cpp", ".c", ".rust", ".vue",
|
||||||
|
".svelte", ".html", ".css", ".scss", ".sass", ".less",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Config files that are important but less than source code
|
||||||
|
let config_files = [
|
||||||
|
"Cargo.toml",
|
||||||
|
"package.json",
|
||||||
|
"go.mod",
|
||||||
|
"go.sum",
|
||||||
|
"pom.xml",
|
||||||
|
"Makefile",
|
||||||
|
"CMakeLists.txt",
|
||||||
|
"build.gradle",
|
||||||
|
"gradle.properties",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Lock files - lowest priority
|
||||||
|
let lock_files = [
|
||||||
|
"Cargo.lock",
|
||||||
|
"package-lock.json",
|
||||||
|
"yarn.lock",
|
||||||
|
"pnpm-lock.yaml",
|
||||||
|
"Gemfile.lock",
|
||||||
|
"composer.lock",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Check lock files first (lowest priority)
|
||||||
|
for lock in lock_files.iter() {
|
||||||
|
if filename.ends_with(lock) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check config files (medium priority)
|
||||||
|
for config in config_files.iter() {
|
||||||
|
if filename.ends_with(config) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check important source files (highest priority)
|
||||||
|
for ext in important_extensions.iter() {
|
||||||
|
if filename.ends_with(ext) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default priority for other files
|
||||||
|
2
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GitRepo {
|
||||||
/// Get unstaged diff
|
/// Get unstaged diff
|
||||||
pub fn get_unstaged_diff(&self) -> Result<String> {
|
pub fn get_unstaged_diff(&self) -> Result<String> {
|
||||||
let diff = self.repo.diff_index_to_workdir(None, None)?;
|
let diff = self.repo.diff_index_to_workdir(None, None)?;
|
||||||
@@ -226,20 +512,23 @@ impl GitRepo {
|
|||||||
|
|
||||||
/// Get list of staged files
|
/// Get list of staged files
|
||||||
pub fn get_staged_files(&self) -> Result<Vec<String>> {
|
pub fn get_staged_files(&self) -> Result<Vec<String>> {
|
||||||
let statuses = self.repo.statuses(Some(
|
let statuses = self
|
||||||
StatusOptions::new()
|
.repo
|
||||||
.include_untracked(false),
|
.statuses(Some(StatusOptions::new().include_untracked(false)))?;
|
||||||
))?;
|
|
||||||
|
|
||||||
let mut files = vec![];
|
let mut files = vec![];
|
||||||
for entry in statuses.iter() {
|
for entry in statuses.iter() {
|
||||||
let status = entry.status();
|
let status = entry.status();
|
||||||
if status.is_index_new() || status.is_index_modified() || status.is_index_deleted() || status.is_index_renamed() || status.is_index_typechange() {
|
if (status.is_index_new()
|
||||||
if let Some(path) = entry.path() {
|
|| status.is_index_modified()
|
||||||
|
|| status.is_index_deleted()
|
||||||
|
|| status.is_index_renamed()
|
||||||
|
|| status.is_index_typechange())
|
||||||
|
&& let Some(path) = entry.path()
|
||||||
|
{
|
||||||
files.push(path.to_string());
|
files.push(path.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(files)
|
Ok(files)
|
||||||
}
|
}
|
||||||
@@ -265,28 +554,21 @@ impl GitRepo {
|
|||||||
|
|
||||||
/// Stage all changes including subdirectories
|
/// Stage all changes including subdirectories
|
||||||
pub fn stage_all(&self) -> Result<()> {
|
pub fn stage_all(&self) -> Result<()> {
|
||||||
let mut index = self.repo.index()?;
|
// Use git command for reliable staging (handles all edge cases)
|
||||||
|
let output = std::process::Command::new("git")
|
||||||
|
.args(["add", "-A"])
|
||||||
|
.current_dir(&self.path)
|
||||||
|
.output()
|
||||||
|
.with_context(|| "Failed to stage changes with git command")?;
|
||||||
|
|
||||||
fn add_directory_recursive(index: &mut git2::Index, base_dir: &Path, current_dir: &Path) -> Result<()> {
|
if !output.status.success() {
|
||||||
for entry in std::fs::read_dir(current_dir)
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
.with_context(|| format!("Failed to read directory: {:?}", current_dir))?
|
bail!("Failed to stage changes: {}", stderr);
|
||||||
{
|
|
||||||
let entry = entry?;
|
|
||||||
let path = entry.path();
|
|
||||||
|
|
||||||
if path.is_file() {
|
|
||||||
if let Ok(rel_path) = path.strip_prefix(base_dir) {
|
|
||||||
let _ = index.add_path(rel_path);
|
|
||||||
}
|
|
||||||
} else if path.is_dir() {
|
|
||||||
add_directory_recursive(index, base_dir, &path)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_directory_recursive(&mut index, &self.path, &self.path)?;
|
// Force refresh the git2 index to pick up changes from git CLI
|
||||||
index.write()?;
|
let _ = self.repo.index()?.write();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,19 +639,30 @@ impl GitRepo {
|
|||||||
let temp_file = tempfile::NamedTempFile::new()?;
|
let temp_file = tempfile::NamedTempFile::new()?;
|
||||||
std::fs::write(temp_file.path(), message)?;
|
std::fs::write(temp_file.path(), message)?;
|
||||||
|
|
||||||
let mut cmd = std::process::Command::new("git");
|
|
||||||
cmd.args(&["commit", "-S", "-F", temp_file.path().to_str().unwrap()])
|
|
||||||
.current_dir(&self.path)
|
|
||||||
.output()?;
|
|
||||||
|
|
||||||
let output = std::process::Command::new("git")
|
let output = std::process::Command::new("git")
|
||||||
.args(&["commit", "-S", "-F", temp_file.path().to_str().unwrap()])
|
.args(["commit", "-S", "-F", temp_file.path().to_str().unwrap()])
|
||||||
.current_dir(&self.path)
|
.current_dir(&self.path)
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
bail!("Failed to create signed commit: {}", stderr);
|
|
||||||
|
let error_msg = if stderr.is_empty() {
|
||||||
|
if stdout.is_empty() {
|
||||||
|
"GPG signing failed. Please check:\n\
|
||||||
|
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||||
|
2. GPG agent is running\n\
|
||||||
|
3. You can sign commits manually (try: git commit -S -m 'test')"
|
||||||
|
.to_string()
|
||||||
|
} else {
|
||||||
|
stdout.to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stderr.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
bail!("Failed to create signed commit: {}", error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let head = self.repo.head()?;
|
let head = self.repo.head()?;
|
||||||
@@ -381,7 +674,8 @@ impl GitRepo {
|
|||||||
let head = self.repo.head()?;
|
let head = self.repo.head()?;
|
||||||
|
|
||||||
if head.is_branch() {
|
if head.is_branch() {
|
||||||
let name = head.shorthand()
|
let name = head
|
||||||
|
.shorthand()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Invalid branch name"))?;
|
.ok_or_else(|| anyhow::anyhow!("Invalid branch name"))?;
|
||||||
Ok(name.to_string())
|
Ok(name.to_string())
|
||||||
} else {
|
} else {
|
||||||
@@ -392,7 +686,8 @@ impl GitRepo {
|
|||||||
/// Get current commit hash (short)
|
/// Get current commit hash (short)
|
||||||
pub fn current_commit_short(&self) -> Result<String> {
|
pub fn current_commit_short(&self) -> Result<String> {
|
||||||
let head = self.repo.head()?;
|
let head = self.repo.head()?;
|
||||||
let oid = head.target()
|
let oid = head
|
||||||
|
.target()
|
||||||
.ok_or_else(|| anyhow::anyhow!("No target for HEAD"))?;
|
.ok_or_else(|| anyhow::anyhow!("No target for HEAD"))?;
|
||||||
Ok(oid.to_string()[..8].to_string())
|
Ok(oid.to_string()[..8].to_string())
|
||||||
}
|
}
|
||||||
@@ -400,7 +695,8 @@ impl GitRepo {
|
|||||||
/// Get current commit hash (full)
|
/// Get current commit hash (full)
|
||||||
pub fn current_commit(&self) -> Result<String> {
|
pub fn current_commit(&self) -> Result<String> {
|
||||||
let head = self.repo.head()?;
|
let head = self.repo.head()?;
|
||||||
let oid = head.target()
|
let oid = head
|
||||||
|
.target()
|
||||||
.ok_or_else(|| anyhow::anyhow!("No target for HEAD"))?;
|
.ok_or_else(|| anyhow::anyhow!("No target for HEAD"))?;
|
||||||
Ok(oid.to_string())
|
Ok(oid.to_string())
|
||||||
}
|
}
|
||||||
@@ -475,12 +771,16 @@ impl GitRepo {
|
|||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
target: oid.to_string(),
|
target: oid.to_string(),
|
||||||
message: commit.message().unwrap_or("").to_string(),
|
message: commit.message().unwrap_or("").to_string(),
|
||||||
|
time: commit.time().seconds(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// Sort tags by time (newest first)
|
||||||
|
tags.sort_by(|a, b| b.time.cmp(&a.time));
|
||||||
|
|
||||||
Ok(tags)
|
Ok(tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,13 +795,7 @@ impl GitRepo {
|
|||||||
if sign {
|
if sign {
|
||||||
self.create_signed_tag_with_git2(name, msg, &sig, target.id())?;
|
self.create_signed_tag_with_git2(name, msg, &sig, target.id())?;
|
||||||
} else {
|
} else {
|
||||||
self.repo.tag(
|
self.repo.tag(name, target.as_object(), &sig, msg, false)?;
|
||||||
name,
|
|
||||||
target.as_object(),
|
|
||||||
&sig,
|
|
||||||
msg,
|
|
||||||
false,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.repo.tag(
|
self.repo.tag(
|
||||||
@@ -517,9 +811,15 @@ impl GitRepo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create signed tag using git CLI
|
/// Create signed tag using git CLI
|
||||||
fn create_signed_tag_with_git2(&self, name: &str, message: &str, _signature: &Signature, _target_id: Oid) -> Result<()> {
|
fn create_signed_tag_with_git2(
|
||||||
|
&self,
|
||||||
|
name: &str,
|
||||||
|
message: &str,
|
||||||
|
_signature: &Signature,
|
||||||
|
_target_id: Oid,
|
||||||
|
) -> Result<()> {
|
||||||
let output = std::process::Command::new("git")
|
let output = std::process::Command::new("git")
|
||||||
.args(&["tag", "-s", name, "-m", message])
|
.args(["tag", "-s", name, "-m", message])
|
||||||
.current_dir(&self.path)
|
.current_dir(&self.path)
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
@@ -532,7 +832,12 @@ impl GitRepo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create GPG signature for arbitrary content
|
/// Create GPG signature for arbitrary content
|
||||||
fn create_gpg_signature_for_content(&self, _content: &str, _gpg_program: &str, _signing_key: &str) -> Result<String> {
|
fn create_gpg_signature_for_content(
|
||||||
|
&self,
|
||||||
|
_content: &str,
|
||||||
|
_gpg_program: &str,
|
||||||
|
_signing_key: &str,
|
||||||
|
) -> Result<String> {
|
||||||
Ok(String::new())
|
Ok(String::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,7 +850,7 @@ impl GitRepo {
|
|||||||
/// Push to remote
|
/// Push to remote
|
||||||
pub fn push(&self, remote: &str, refspec: &str) -> Result<()> {
|
pub fn push(&self, remote: &str, refspec: &str) -> Result<()> {
|
||||||
let output = std::process::Command::new("git")
|
let output = std::process::Command::new("git")
|
||||||
.args(&["push", remote, refspec])
|
.args(["push", remote, refspec])
|
||||||
.current_dir(&self.path)
|
.current_dir(&self.path)
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
@@ -560,7 +865,8 @@ impl GitRepo {
|
|||||||
/// Get remote URL
|
/// Get remote URL
|
||||||
pub fn get_remote_url(&self, remote: &str) -> Result<String> {
|
pub fn get_remote_url(&self, remote: &str) -> Result<String> {
|
||||||
let remote_obj = self.repo.find_remote(remote)?;
|
let remote_obj = self.repo.find_remote(remote)?;
|
||||||
let url = remote_obj.url()
|
let url = remote_obj
|
||||||
|
.url()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Remote has no URL"))?;
|
.ok_or_else(|| anyhow::anyhow!("Remote has no URL"))?;
|
||||||
Ok(url.to_string())
|
Ok(url.to_string())
|
||||||
}
|
}
|
||||||
@@ -572,35 +878,53 @@ impl GitRepo {
|
|||||||
|
|
||||||
/// Get repository status summary
|
/// Get repository status summary
|
||||||
pub fn status_summary(&self) -> Result<StatusSummary> {
|
pub fn status_summary(&self) -> Result<StatusSummary> {
|
||||||
let statuses = self.repo.statuses(Some(StatusOptions::new().include_untracked(true)))?;
|
// Use git CLI for more reliable status detection
|
||||||
|
let output = std::process::Command::new("git")
|
||||||
|
.args(["status", "--porcelain"])
|
||||||
|
.current_dir(&self.path)
|
||||||
|
.output()
|
||||||
|
.with_context(|| "Failed to get status with git command")?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
bail!("Failed to get status: {}", stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
let mut staged = 0;
|
let mut staged = 0;
|
||||||
let mut unstaged = 0;
|
let mut unstaged = 0;
|
||||||
let mut untracked = 0;
|
let mut untracked = 0;
|
||||||
let mut conflicted = 0;
|
let mut conflicted = 0;
|
||||||
|
|
||||||
for entry in statuses.iter() {
|
for line in stdout.lines() {
|
||||||
let status = entry.status();
|
if line.len() >= 2 {
|
||||||
|
let index_status = line.chars().next().unwrap();
|
||||||
|
let worktree_status = line.chars().nth(1).unwrap();
|
||||||
|
|
||||||
if status.is_index_new() || status.is_index_modified() ||
|
// Staged changes (first column not space)
|
||||||
status.is_index_deleted() || status.is_index_renamed() ||
|
if index_status != ' ' && index_status != '?' {
|
||||||
status.is_index_typechange() {
|
|
||||||
staged += 1;
|
staged += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.is_wt_modified() || status.is_wt_deleted() ||
|
// Unstaged changes (second column not space)
|
||||||
status.is_wt_renamed() || status.is_wt_typechange() {
|
if worktree_status != ' ' && worktree_status != '?' {
|
||||||
unstaged += 1;
|
unstaged += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.is_wt_new() {
|
// Untracked files (both columns are ?)
|
||||||
|
if index_status == '?' && worktree_status == '?' {
|
||||||
untracked += 1;
|
untracked += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.is_conflicted() {
|
// Conflicted files (both columns are U or DD, AA, etc.)
|
||||||
|
if (index_status == 'U' || worktree_status == 'U')
|
||||||
|
|| (index_status == 'A' && worktree_status == 'A')
|
||||||
|
|| (index_status == 'D' && worktree_status == 'D')
|
||||||
|
{
|
||||||
conflicted += 1;
|
conflicted += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(StatusSummary {
|
Ok(StatusSummary {
|
||||||
staged,
|
staged,
|
||||||
@@ -648,6 +972,7 @@ pub struct TagInfo {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub target: String,
|
pub target: String,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
pub time: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Repository status summary
|
/// Repository status summary
|
||||||
@@ -684,23 +1009,74 @@ impl StatusSummary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find git repository starting from path and walking up
|
|
||||||
pub fn find_repo<P: AsRef<Path>>(start_path: P) -> Result<GitRepo> {
|
pub fn find_repo<P: AsRef<Path>>(start_path: P) -> Result<GitRepo> {
|
||||||
let start_path = start_path.as_ref();
|
let start_path = start_path.as_ref();
|
||||||
|
|
||||||
if let Ok(repo) = GitRepo::open(start_path) {
|
let absolute_start = get_absolute_path(start_path)?;
|
||||||
|
let resolved_start = resolve_path_without_canonicalize(&absolute_start);
|
||||||
|
|
||||||
|
if let Ok(repo) = GitRepo::open(&resolved_start) {
|
||||||
return Ok(repo);
|
return Ok(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut current = start_path;
|
let mut current = resolved_start.as_path();
|
||||||
|
let mut attempted_paths = vec![current.to_string_lossy().to_string()];
|
||||||
|
|
||||||
|
let max_depth = 50;
|
||||||
|
let mut depth = 0;
|
||||||
|
|
||||||
while let Some(parent) = current.parent() {
|
while let Some(parent) = current.parent() {
|
||||||
|
depth += 1;
|
||||||
|
if depth > max_depth {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
attempted_paths.push(parent.to_string_lossy().to_string());
|
||||||
|
|
||||||
if let Ok(repo) = GitRepo::open(parent) {
|
if let Ok(repo) = GitRepo::open(parent) {
|
||||||
return Ok(repo);
|
return Ok(repo);
|
||||||
}
|
}
|
||||||
current = parent;
|
current = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("No git repository found starting from {:?}", start_path)
|
if let Ok(output) = std::process::Command::new("git")
|
||||||
|
.args(["rev-parse", "--show-toplevel"])
|
||||||
|
.current_dir(&resolved_start)
|
||||||
|
.output()
|
||||||
|
&& output.status.success()
|
||||||
|
{
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let git_root = stdout.trim();
|
||||||
|
if !git_root.is_empty()
|
||||||
|
&& let Ok(repo) = GitRepo::open(git_root)
|
||||||
|
{
|
||||||
|
return Ok(repo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let diagnosis = diagnose_repo_issue(&resolved_start);
|
||||||
|
|
||||||
|
bail!(
|
||||||
|
"No git repository found.\n\
|
||||||
|
\n\
|
||||||
|
=== Starting Path ===\n {:?}\n\
|
||||||
|
\n\
|
||||||
|
=== Paths Attempted ===\n {}\n\
|
||||||
|
\n\
|
||||||
|
=== Current Directory ===\n {:?}\n\
|
||||||
|
\n\
|
||||||
|
=== Diagnosis ===\n {}\n\
|
||||||
|
\n\
|
||||||
|
=== Suggestions ===\n\
|
||||||
|
1. Ensure you are inside a git repository (run: git status)\n\
|
||||||
|
2. Initialize a new repo: git init\n\
|
||||||
|
3. Clone an existing repo: git clone <url>\n\
|
||||||
|
4. Check if .git directory exists and is accessible",
|
||||||
|
resolved_start,
|
||||||
|
attempted_paths.join("\n "),
|
||||||
|
std::env::current_dir().unwrap_or_default(),
|
||||||
|
diagnosis
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if path is inside a git repository
|
/// Check if path is inside a git repository
|
||||||
@@ -801,6 +1177,105 @@ impl<'a> GitConfigHelper<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configuration source indicator
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum ConfigSource {
|
||||||
|
Local,
|
||||||
|
Global,
|
||||||
|
NotSet,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for ConfigSource {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
ConfigSource::Local => write!(f, "local"),
|
||||||
|
ConfigSource::Global => write!(f, "global"),
|
||||||
|
ConfigSource::NotSet => write!(f, "not set"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Single configuration entry with source information
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ConfigEntry {
|
||||||
|
pub value: Option<String>,
|
||||||
|
pub source: ConfigSource,
|
||||||
|
pub local_value: Option<String>,
|
||||||
|
pub global_value: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConfigEntry {
|
||||||
|
pub fn new(local: Option<String>, global: Option<String>) -> Self {
|
||||||
|
let (value, source) = match (&local, &global) {
|
||||||
|
(Some(_), _) => (local.clone(), ConfigSource::Local),
|
||||||
|
(None, Some(_)) => (global.clone(), ConfigSource::Global),
|
||||||
|
(None, None) => (None, ConfigSource::NotSet),
|
||||||
|
};
|
||||||
|
Self {
|
||||||
|
value,
|
||||||
|
source,
|
||||||
|
local_value: local,
|
||||||
|
global_value: global,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_set(&self) -> bool {
|
||||||
|
self.value.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_local(&self) -> bool {
|
||||||
|
self.source == ConfigSource::Local
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_global(&self) -> bool {
|
||||||
|
self.source == ConfigSource::Global
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Merged user configuration with local/global source tracking
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct MergedUserConfig {
|
||||||
|
pub name: ConfigEntry,
|
||||||
|
pub email: ConfigEntry,
|
||||||
|
pub signing_key: ConfigEntry,
|
||||||
|
pub ssh_command: ConfigEntry,
|
||||||
|
pub commit_gpgsign: ConfigEntry,
|
||||||
|
pub tag_gpgsign: ConfigEntry,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MergedUserConfig {
|
||||||
|
pub fn from_repo(repo: &Repository) -> Result<Self> {
|
||||||
|
let local_config = repo.config().ok();
|
||||||
|
let global_config = git2::Config::open_default().ok();
|
||||||
|
|
||||||
|
let get_entry = |key: &str| -> ConfigEntry {
|
||||||
|
let local = local_config.as_ref().and_then(|c| c.get_string(key).ok());
|
||||||
|
let global = global_config.as_ref().and_then(|c| c.get_string(key).ok());
|
||||||
|
ConfigEntry::new(local, global)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
name: get_entry("user.name"),
|
||||||
|
email: get_entry("user.email"),
|
||||||
|
signing_key: get_entry("user.signingkey"),
|
||||||
|
ssh_command: get_entry("core.sshCommand"),
|
||||||
|
commit_gpgsign: get_entry("commit.gpgsign"),
|
||||||
|
tag_gpgsign: get_entry("tag.gpgsign"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_complete(&self) -> bool {
|
||||||
|
self.name.is_set() && self.email.is_set()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_local_overrides(&self) -> bool {
|
||||||
|
self.name.is_local()
|
||||||
|
|| self.email.is_local()
|
||||||
|
|| self.signing_key.is_local()
|
||||||
|
|| self.ssh_command.is_local()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// User configuration for git
|
/// User configuration for git
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct UserConfig {
|
pub struct UserConfig {
|
||||||
@@ -824,23 +1299,38 @@ impl UserConfig {
|
|||||||
diffs.push(ConfigDiff {
|
diffs.push(ConfigDiff {
|
||||||
key: "user.name".to_string(),
|
key: "user.name".to_string(),
|
||||||
left: self.name.clone().unwrap_or_else(|| "<not set>".to_string()),
|
left: self.name.clone().unwrap_or_else(|| "<not set>".to_string()),
|
||||||
right: other.name.clone().unwrap_or_else(|| "<not set>".to_string()),
|
right: other
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.email != other.email {
|
if self.email != other.email {
|
||||||
diffs.push(ConfigDiff {
|
diffs.push(ConfigDiff {
|
||||||
key: "user.email".to_string(),
|
key: "user.email".to_string(),
|
||||||
left: self.email.clone().unwrap_or_else(|| "<not set>".to_string()),
|
left: self
|
||||||
right: other.email.clone().unwrap_or_else(|| "<not set>".to_string()),
|
.email
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
|
right: other
|
||||||
|
.email
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.signing_key != other.signing_key {
|
if self.signing_key != other.signing_key {
|
||||||
diffs.push(ConfigDiff {
|
diffs.push(ConfigDiff {
|
||||||
key: "user.signingkey".to_string(),
|
key: "user.signingkey".to_string(),
|
||||||
left: self.signing_key.clone().unwrap_or_else(|| "<not set>".to_string()),
|
left: self
|
||||||
right: other.signing_key.clone().unwrap_or_else(|| "<not set>".to_string()),
|
.signing_key
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
|
right: other
|
||||||
|
.signing_key
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "<not set>".to_string()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::GitRepo;
|
use super::GitRepo;
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Result, bail};
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
|
|
||||||
/// Tag builder for creating tags
|
/// Tag builder for creating tags
|
||||||
@@ -9,7 +9,6 @@ pub struct TagBuilder {
|
|||||||
annotate: bool,
|
annotate: bool,
|
||||||
sign: bool,
|
sign: bool,
|
||||||
force: bool,
|
force: bool,
|
||||||
dry_run: bool,
|
|
||||||
version_prefix: String,
|
version_prefix: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +21,6 @@ impl TagBuilder {
|
|||||||
annotate: true,
|
annotate: true,
|
||||||
sign: false,
|
sign: false,
|
||||||
force: false,
|
force: false,
|
||||||
dry_run: false,
|
|
||||||
version_prefix: "v".to_string(),
|
version_prefix: "v".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,12 +55,6 @@ impl TagBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dry run (don't actually create tag)
|
|
||||||
pub fn dry_run(mut self, dry_run: bool) -> Self {
|
|
||||||
self.dry_run = dry_run;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set version prefix
|
/// Set version prefix
|
||||||
pub fn version_prefix(mut self, prefix: impl Into<String>) -> Self {
|
pub fn version_prefix(mut self, prefix: impl Into<String>) -> Self {
|
||||||
self.version_prefix = prefix.into();
|
self.version_prefix = prefix.into();
|
||||||
@@ -77,9 +69,7 @@ impl TagBuilder {
|
|||||||
|
|
||||||
/// Build tag message
|
/// Build tag message
|
||||||
pub fn build_message(&self) -> Result<String> {
|
pub fn build_message(&self) -> Result<String> {
|
||||||
let message = self.message.as_ref()
|
let message = self.message.as_ref().cloned().unwrap_or_else(|| {
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
let name = self.name.as_deref().unwrap_or("unknown");
|
let name = self.name.as_deref().unwrap_or("unknown");
|
||||||
format!("Release {}", name)
|
format!("Release {}", name)
|
||||||
});
|
});
|
||||||
@@ -89,18 +79,11 @@ impl TagBuilder {
|
|||||||
|
|
||||||
/// Execute tag creation
|
/// Execute tag creation
|
||||||
pub fn execute(&self, repo: &GitRepo) -> Result<()> {
|
pub fn execute(&self, repo: &GitRepo) -> Result<()> {
|
||||||
let name = self.name.as_ref()
|
let name = self
|
||||||
|
.name
|
||||||
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow::anyhow!("Tag name is required"))?;
|
.ok_or_else(|| anyhow::anyhow!("Tag name is required"))?;
|
||||||
|
|
||||||
if self.dry_run {
|
|
||||||
println!("Would create tag: {}", name);
|
|
||||||
if self.annotate {
|
|
||||||
println!("Message: {}", self.build_message()?);
|
|
||||||
}
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if tag already exists
|
|
||||||
if !self.force {
|
if !self.force {
|
||||||
let existing_tags = repo.get_tags()?;
|
let existing_tags = repo.get_tags()?;
|
||||||
if existing_tags.iter().any(|t| t.name == *name) {
|
if existing_tags.iter().any(|t| t.name == *name) {
|
||||||
@@ -153,7 +136,10 @@ impl VersionBump {
|
|||||||
"minor" => Ok(Self::Minor),
|
"minor" => Ok(Self::Minor),
|
||||||
"patch" => Ok(Self::Patch),
|
"patch" => Ok(Self::Patch),
|
||||||
"prerelease" | "pre" => Ok(Self::Prerelease),
|
"prerelease" | "pre" => Ok(Self::Prerelease),
|
||||||
_ => bail!("Invalid version bump: {}. Use: major, minor, patch, prerelease", s),
|
_ => bail!(
|
||||||
|
"Invalid version bump: {}. Use: major, minor, patch, prerelease",
|
||||||
|
s
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +190,10 @@ pub fn suggest_version_bump(commits: &[super::CommitInfo]) -> VersionBump {
|
|||||||
for commit in commits {
|
for commit in commits {
|
||||||
let msg = commit.message.to_lowercase();
|
let msg = commit.message.to_lowercase();
|
||||||
|
|
||||||
if msg.contains("breaking change") || msg.contains("breaking-change") || msg.contains("breaking_change") {
|
if msg.contains("breaking change")
|
||||||
|
|| msg.contains("breaking-change")
|
||||||
|
|| msg.contains("breaking_change")
|
||||||
|
{
|
||||||
has_breaking = true;
|
has_breaking = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,8 +287,9 @@ pub fn delete_tag(repo: &GitRepo, name: &str, remote: Option<&str>) -> Result<()
|
|||||||
if let Some(remote) = remote {
|
if let Some(remote) = remote {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
|
let refspec = format!(":refs/tags/{}", name);
|
||||||
let output = Command::new("git")
|
let output = Command::new("git")
|
||||||
.args(&["push", remote, ":refs/tags/{}"])
|
.args(["push", remote, &refspec])
|
||||||
.current_dir(repo.path())
|
.current_dir(repo.path())
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
|
|||||||
1172
src/i18n/messages.rs
Normal file
1172
src/i18n/messages.rs
Normal file
File diff suppressed because it is too large
Load Diff
5
src/i18n/mod.rs
Normal file
5
src/i18n/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod messages;
|
||||||
|
pub mod translator;
|
||||||
|
|
||||||
|
pub use messages::Messages;
|
||||||
|
pub use translator::translate_changelog_category;
|
||||||
241
src/i18n/translator.rs
Normal file
241
src/i18n/translator.rs
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
use crate::config::Language;
|
||||||
|
|
||||||
|
pub struct Translator {
|
||||||
|
language: Language,
|
||||||
|
keep_types_english: bool,
|
||||||
|
keep_changelog_types_english: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Translator {
|
||||||
|
pub fn new(
|
||||||
|
language: Language,
|
||||||
|
keep_types_english: bool,
|
||||||
|
keep_changelog_types_english: bool,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
language,
|
||||||
|
keep_types_english,
|
||||||
|
keep_changelog_types_english,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_commit_type(&self, commit_type: &str) -> String {
|
||||||
|
if self.keep_types_english {
|
||||||
|
return commit_type.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.language {
|
||||||
|
Language::English => commit_type.to_string(),
|
||||||
|
Language::Chinese => self.translate_commit_type_zh(commit_type),
|
||||||
|
Language::Japanese => self.translate_commit_type_ja(commit_type),
|
||||||
|
Language::Korean => self.translate_commit_type_ko(commit_type),
|
||||||
|
Language::Spanish => self.translate_commit_type_es(commit_type),
|
||||||
|
Language::French => self.translate_commit_type_fr(commit_type),
|
||||||
|
Language::German => self.translate_commit_type_de(commit_type),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_changelog_category(&self, category: &str) -> String {
|
||||||
|
if self.keep_changelog_types_english {
|
||||||
|
return category.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.language {
|
||||||
|
Language::English => category.to_string(),
|
||||||
|
Language::Chinese => self.translate_changelog_category_zh(category),
|
||||||
|
Language::Japanese => self.translate_changelog_category_ja(category),
|
||||||
|
Language::Korean => self.translate_changelog_category_ko(category),
|
||||||
|
Language::Spanish => self.translate_changelog_category_es(category),
|
||||||
|
Language::French => self.translate_changelog_category_fr(category),
|
||||||
|
Language::German => self.translate_changelog_category_de(category),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_zh(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "新功能".to_string(),
|
||||||
|
"fix" => "修复".to_string(),
|
||||||
|
"docs" => "文档".to_string(),
|
||||||
|
"style" => "样式".to_string(),
|
||||||
|
"refactor" => "重构".to_string(),
|
||||||
|
"perf" => "性能".to_string(),
|
||||||
|
"test" => "测试".to_string(),
|
||||||
|
"build" => "构建".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "杂项".to_string(),
|
||||||
|
"revert" => "回滚".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_ja(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "機能".to_string(),
|
||||||
|
"fix" => "修正".to_string(),
|
||||||
|
"docs" => "ドキュメント".to_string(),
|
||||||
|
"style" => "スタイル".to_string(),
|
||||||
|
"refactor" => "リファクタリング".to_string(),
|
||||||
|
"perf" => "パフォーマンス".to_string(),
|
||||||
|
"test" => "テスト".to_string(),
|
||||||
|
"build" => "ビルド".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "雑務".to_string(),
|
||||||
|
"revert" => "取り消し".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_ko(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "기능".to_string(),
|
||||||
|
"fix" => "버그 수정".to_string(),
|
||||||
|
"docs" => "문서".to_string(),
|
||||||
|
"style" => "스타일".to_string(),
|
||||||
|
"refactor" => "리팩토링".to_string(),
|
||||||
|
"perf" => "성능".to_string(),
|
||||||
|
"test" => "테스트".to_string(),
|
||||||
|
"build" => "빌드".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "기타".to_string(),
|
||||||
|
"revert" => "되돌리기".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_es(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "nueva función".to_string(),
|
||||||
|
"fix" => "corrección".to_string(),
|
||||||
|
"docs" => "documentación".to_string(),
|
||||||
|
"style" => "estilo".to_string(),
|
||||||
|
"refactor" => "refactorización".to_string(),
|
||||||
|
"perf" => "rendimiento".to_string(),
|
||||||
|
"test" => "pruebas".to_string(),
|
||||||
|
"build" => "construcción".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "tareas".to_string(),
|
||||||
|
"revert" => "revertir".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_fr(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "nouvelle fonctionnalité".to_string(),
|
||||||
|
"fix" => "correction".to_string(),
|
||||||
|
"docs" => "documentation".to_string(),
|
||||||
|
"style" => "style".to_string(),
|
||||||
|
"refactor" => "refactorisation".to_string(),
|
||||||
|
"perf" => "performance".to_string(),
|
||||||
|
"test" => "tests".to_string(),
|
||||||
|
"build" => "construction".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "tâches".to_string(),
|
||||||
|
"revert" => "rétablir".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_commit_type_de(&self, commit_type: &str) -> String {
|
||||||
|
match commit_type {
|
||||||
|
"feat" => "Neue Funktion".to_string(),
|
||||||
|
"fix" => "Korrektur".to_string(),
|
||||||
|
"docs" => "Dokumentation".to_string(),
|
||||||
|
"style" => "Stil".to_string(),
|
||||||
|
"refactor" => "Refactoring".to_string(),
|
||||||
|
"perf" => "Leistung".to_string(),
|
||||||
|
"test" => "Tests".to_string(),
|
||||||
|
"build" => "Build".to_string(),
|
||||||
|
"ci" => "CI".to_string(),
|
||||||
|
"chore" => "Wartung".to_string(),
|
||||||
|
"revert" => "Zurücksetzen".to_string(),
|
||||||
|
_ => commit_type.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_zh(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "新增".to_string(),
|
||||||
|
"changed" => "更改".to_string(),
|
||||||
|
"deprecated" => "弃用".to_string(),
|
||||||
|
"removed" => "移除".to_string(),
|
||||||
|
"fixed" => "修复".to_string(),
|
||||||
|
"security" => "安全".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_ja(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "追加".to_string(),
|
||||||
|
"changed" => "変更".to_string(),
|
||||||
|
"deprecated" => "非推奨".to_string(),
|
||||||
|
"removed" => "削除".to_string(),
|
||||||
|
"fixed" => "修正".to_string(),
|
||||||
|
"security" => "セキュリティ".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_ko(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "추가됨".to_string(),
|
||||||
|
"changed" => "변경됨".to_string(),
|
||||||
|
"deprecated" => "사용 중단".to_string(),
|
||||||
|
"removed" => "제거됨".to_string(),
|
||||||
|
"fixed" => "수정됨".to_string(),
|
||||||
|
"security" => "보안".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_es(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "Agregado".to_string(),
|
||||||
|
"changed" => "Cambiado".to_string(),
|
||||||
|
"deprecated" => "Obsoleto".to_string(),
|
||||||
|
"removed" => "Eliminado".to_string(),
|
||||||
|
"fixed" => "Corregido".to_string(),
|
||||||
|
"security" => "Seguridad".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_fr(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "Ajouté".to_string(),
|
||||||
|
"changed" => "Modifié".to_string(),
|
||||||
|
"deprecated" => "Obsolète".to_string(),
|
||||||
|
"removed" => "Supprimé".to_string(),
|
||||||
|
"fixed" => "Corrigé".to_string(),
|
||||||
|
"security" => "Sécurité".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_changelog_category_de(&self, category: &str) -> String {
|
||||||
|
match category.to_lowercase().as_str() {
|
||||||
|
"added" => "Hinzugefügt".to_string(),
|
||||||
|
"changed" => "Geändert".to_string(),
|
||||||
|
"deprecated" => "Veraltet".to_string(),
|
||||||
|
"removed" => "Entfernt".to_string(),
|
||||||
|
"fixed" => "Behoben".to_string(),
|
||||||
|
"security" => "Sicherheit".to_string(),
|
||||||
|
_ => category.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_commit_type(commit_type: &str, language: Language, keep_english: bool) -> String {
|
||||||
|
let translator = Translator::new(language, keep_english, true);
|
||||||
|
translator.translate_commit_type(commit_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_changelog_category(
|
||||||
|
category: &str,
|
||||||
|
language: Language,
|
||||||
|
keep_english: bool,
|
||||||
|
) -> String {
|
||||||
|
let translator = Translator::new(language, true, keep_english);
|
||||||
|
translator.translate_changelog_category(category)
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::thinking::ThinkingStateManager;
|
||||||
use anyhow::{bail, Context, Result};
|
use super::{LlmProvider, create_http_client};
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Anthropic Claude API client
|
/// Anthropic Claude API client
|
||||||
@@ -9,6 +11,12 @@ pub struct AnthropicClient {
|
|||||||
api_key: String,
|
api_key: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
thinking_enabled: bool,
|
||||||
|
thinking_budget_tokens: u32,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
top_p: Option<f32>,
|
||||||
|
thinking_state: Option<Arc<ThinkingStateManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -17,24 +25,59 @@ struct MessagesRequest {
|
|||||||
max_tokens: u32,
|
max_tokens: u32,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
temperature: Option<f32>,
|
temperature: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
top_p: Option<f32>,
|
||||||
messages: Vec<AnthropicMessage>,
|
messages: Vec<AnthropicMessage>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
system: Option<String>,
|
system: Option<Vec<SystemContent>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
thinking: Option<ThinkingConfig>,
|
||||||
|
stream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
|
struct SystemContent {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
content_type: String,
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
struct ThinkingConfig {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
thinking_type: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
budget_tokens: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
struct AnthropicMessage {
|
struct AnthropicMessage {
|
||||||
role: String,
|
role: String,
|
||||||
content: String,
|
content: AnthropicContent,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum AnthropicContent {
|
||||||
|
Text(String),
|
||||||
|
Blocks(Vec<ContentBlock>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
struct ContentBlock {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
content_type: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
text: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct MessagesResponse {
|
struct MessagesResponse {
|
||||||
content: Vec<ContentBlock>,
|
content: Vec<ResponseContentBlock>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct ContentBlock {
|
struct ResponseContentBlock {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
content_type: String,
|
content_type: String,
|
||||||
text: String,
|
text: String,
|
||||||
@@ -52,8 +95,57 @@ struct AnthropicError {
|
|||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Streaming SSE event structures ---
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct SseEvent {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
event_type: String,
|
||||||
|
#[serde(default)]
|
||||||
|
message: Option<SseMessage>,
|
||||||
|
#[serde(default)]
|
||||||
|
index: Option<u32>,
|
||||||
|
#[serde(default)]
|
||||||
|
content_block: Option<SseContentBlock>,
|
||||||
|
#[serde(default)]
|
||||||
|
delta: Option<SseDelta>,
|
||||||
|
#[serde(default)]
|
||||||
|
usage: Option<SseUsage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct SseMessage {
|
||||||
|
#[serde(default)]
|
||||||
|
content: Option<Vec<SseContentBlock>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct SseContentBlock {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
content_type: String,
|
||||||
|
#[serde(default)]
|
||||||
|
thinking: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
text: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct SseDelta {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
delta_type: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
thinking: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
text: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct SseUsage {
|
||||||
|
#[serde(default)]
|
||||||
|
output_tokens: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
impl AnthropicClient {
|
impl AnthropicClient {
|
||||||
/// Create new Anthropic client
|
|
||||||
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(60))?;
|
||||||
|
|
||||||
@@ -61,16 +153,54 @@ impl AnthropicClient {
|
|||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
thinking_budget_tokens: 1024,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout
|
|
||||||
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
||||||
self.client = create_http_client(timeout)?;
|
self.client = create_http_client(timeout)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate API key
|
pub fn with_thinking(mut self, enabled: bool) -> Self {
|
||||||
|
self.thinking_enabled = enabled;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_thinking_budget_tokens(mut self, budget_tokens: u32) -> Self {
|
||||||
|
self.thinking_budget_tokens = budget_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_top_p(mut self, top_p: f32) -> Self {
|
||||||
|
self.top_p = Some(top_p);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_thinking_state(mut self, state: Arc<ThinkingStateManager>) -> Self {
|
||||||
|
self.thinking_state = Some(state);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
|
Ok(ANTHROPIC_MODELS.iter().map(|&m| m.to_string()).collect())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn validate_key(&self) -> Result<bool> {
|
pub async fn validate_key(&self) -> Result<bool> {
|
||||||
let url = "https://api.anthropic.com/v1/messages";
|
let url = "https://api.anthropic.com/v1/messages";
|
||||||
|
|
||||||
@@ -78,14 +208,18 @@ impl AnthropicClient {
|
|||||||
model: self.model.clone(),
|
model: self.model.clone(),
|
||||||
max_tokens: 5,
|
max_tokens: 5,
|
||||||
temperature: Some(0.0),
|
temperature: Some(0.0),
|
||||||
|
top_p: None,
|
||||||
messages: vec![AnthropicMessage {
|
messages: vec![AnthropicMessage {
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: "Hi".to_string(),
|
content: AnthropicContent::Text("Hi".to_string()),
|
||||||
}],
|
}],
|
||||||
system: None,
|
system: None,
|
||||||
|
thinking: None,
|
||||||
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header("x-api-key", &self.api_key)
|
.header("x-api-key", &self.api_key)
|
||||||
.header("anthropic-version", "2023-06-01")
|
.header("anthropic-version", "2023-06-01")
|
||||||
@@ -118,25 +252,28 @@ impl LlmProvider for AnthropicClient {
|
|||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![AnthropicMessage {
|
let messages = vec![AnthropicMessage {
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: AnthropicContent::Text(prompt.to_string()),
|
||||||
}];
|
}];
|
||||||
|
|
||||||
self.messages_request(messages, None).await
|
self.messages_request_with_retry(messages, None).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
||||||
let messages = vec![AnthropicMessage {
|
let messages = vec![AnthropicMessage {
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: user.to_string(),
|
content: AnthropicContent::Text(user.to_string()),
|
||||||
}];
|
}];
|
||||||
|
|
||||||
let system = if system.is_empty() {
|
let system = if system.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(system.to_string())
|
Some(vec![SystemContent {
|
||||||
|
content_type: "text".to_string(),
|
||||||
|
text: system.to_string(),
|
||||||
|
}])
|
||||||
};
|
};
|
||||||
|
|
||||||
self.messages_request(messages, system).await
|
self.messages_request_with_retry(messages, system).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_available(&self) -> bool {
|
async fn is_available(&self) -> bool {
|
||||||
@@ -149,22 +286,84 @@ impl LlmProvider for AnthropicClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AnthropicClient {
|
impl AnthropicClient {
|
||||||
|
async fn messages_request_with_retry(
|
||||||
|
&self,
|
||||||
|
messages: Vec<AnthropicMessage>,
|
||||||
|
system: Option<Vec<SystemContent>>,
|
||||||
|
) -> Result<String> {
|
||||||
|
let mut last_error = None;
|
||||||
|
|
||||||
|
for attempt in 1..=3 {
|
||||||
|
match self
|
||||||
|
.messages_request(messages.clone(), system.clone())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(result) => return Ok(result),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = e.to_string();
|
||||||
|
let is_retryable = err_msg.contains("timeout")
|
||||||
|
|| err_msg.contains("connection")
|
||||||
|
|| err_msg.contains("temporary")
|
||||||
|
|| err_msg.contains("5")
|
||||||
|
&& (err_msg.contains("500")
|
||||||
|
|| err_msg.contains("502")
|
||||||
|
|| err_msg.contains("503")
|
||||||
|
|| err_msg.contains("504"));
|
||||||
|
|
||||||
|
if !is_retryable || attempt == 3 {
|
||||||
|
last_error = Some(e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(500 * 2u64.pow(attempt - 1))).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Request failed after retries")))
|
||||||
|
}
|
||||||
|
|
||||||
async fn messages_request(
|
async fn messages_request(
|
||||||
&self,
|
&self,
|
||||||
messages: Vec<AnthropicMessage>,
|
messages: Vec<AnthropicMessage>,
|
||||||
system: Option<String>,
|
system: Option<Vec<SystemContent>>,
|
||||||
|
) -> Result<String> {
|
||||||
|
if self.thinking_enabled {
|
||||||
|
self.streaming_messages_request(messages, system).await
|
||||||
|
} else {
|
||||||
|
self.non_streaming_messages_request(messages, system).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn non_streaming_messages_request(
|
||||||
|
&self,
|
||||||
|
messages: Vec<AnthropicMessage>,
|
||||||
|
system: Option<Vec<SystemContent>>,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let url = "https://api.anthropic.com/v1/messages";
|
let url = "https://api.anthropic.com/v1/messages";
|
||||||
|
|
||||||
let request = MessagesRequest {
|
let temperature = if self.temperature == 0.0 {
|
||||||
model: self.model.clone(),
|
None
|
||||||
max_tokens: 500,
|
} else {
|
||||||
temperature: Some(0.7),
|
Some(self.temperature)
|
||||||
messages,
|
|
||||||
system,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let request = MessagesRequest {
|
||||||
|
model: self.model.clone(),
|
||||||
|
max_tokens: self.max_tokens,
|
||||||
|
temperature,
|
||||||
|
top_p: self.top_p,
|
||||||
|
messages,
|
||||||
|
system,
|
||||||
|
thinking: Some(ThinkingConfig {
|
||||||
|
thinking_type: "disabled".to_string(),
|
||||||
|
budget_tokens: None,
|
||||||
|
}),
|
||||||
|
stream: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header("x-api-key", &self.api_key)
|
.header("x-api-key", &self.api_key)
|
||||||
.header("anthropic-version", "2023-06-01")
|
.header("anthropic-version", "2023-06-01")
|
||||||
@@ -179,9 +378,12 @@ impl AnthropicClient {
|
|||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
// Try to parse error
|
|
||||||
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
bail!("Anthropic API error: {} ({})", error.error.message, error.error.error_type);
|
bail!(
|
||||||
|
"Anthropic API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("Anthropic API error: {} - {}", status, text);
|
bail!("Anthropic API error: {} - {}", status, text);
|
||||||
@@ -192,16 +394,183 @@ impl AnthropicClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse Anthropic response")?;
|
.context("Failed to parse Anthropic response")?;
|
||||||
|
|
||||||
result.content
|
result
|
||||||
|
.content
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|c| c.content_type == "text")
|
.find(|c| c.content_type == "text")
|
||||||
.map(|c| c.text.trim().to_string())
|
.map(|c| c.text.trim().to_string())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No text response from Anthropic"))
|
.ok_or_else(|| anyhow::anyhow!("No text response from Anthropic"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Streaming request for thinking mode, filters thinking content blocks
|
||||||
|
async fn streaming_messages_request(
|
||||||
|
&self,
|
||||||
|
messages: Vec<AnthropicMessage>,
|
||||||
|
system: Option<Vec<SystemContent>>,
|
||||||
|
) -> Result<String> {
|
||||||
|
let url = "https://api.anthropic.com/v1/messages";
|
||||||
|
|
||||||
|
let thinking = ThinkingConfig {
|
||||||
|
thinking_type: "enabled".to_string(),
|
||||||
|
budget_tokens: Some(self.thinking_budget_tokens),
|
||||||
|
};
|
||||||
|
|
||||||
|
// max_tokens must exceed budget_tokens
|
||||||
|
let max_tokens = (self.max_tokens).max(self.thinking_budget_tokens + 100);
|
||||||
|
|
||||||
|
let request = MessagesRequest {
|
||||||
|
model: self.model.clone(),
|
||||||
|
max_tokens,
|
||||||
|
temperature: None, // must be omitted for thinking mode
|
||||||
|
top_p: None,
|
||||||
|
messages,
|
||||||
|
system,
|
||||||
|
thinking: Some(thinking),
|
||||||
|
stream: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(url)
|
||||||
|
.header("x-api-key", &self.api_key)
|
||||||
|
.header("anthropic-version", "2023-06-01")
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "text/event-stream")
|
||||||
|
.json(&request)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send streaming request to Anthropic")?;
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
|
bail!(
|
||||||
|
"Anthropic API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("Anthropic API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut content_buffer = String::new();
|
||||||
|
let mut in_thinking = false;
|
||||||
|
let mut has_reasoning = false;
|
||||||
|
let mut has_content = false;
|
||||||
|
|
||||||
|
let thinking_state = self.thinking_state.as_ref();
|
||||||
|
|
||||||
|
let mut byte_stream = response.bytes_stream();
|
||||||
|
let mut line_buffer = String::new();
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
|
while let Some(chunk) = byte_stream.next().await {
|
||||||
|
let chunk = chunk.context("Failed to read streaming response chunk")?;
|
||||||
|
let chunk_str =
|
||||||
|
String::from_utf8(chunk.to_vec()).context("Invalid UTF-8 in stream chunk")?;
|
||||||
|
|
||||||
|
line_buffer.push_str(&chunk_str);
|
||||||
|
|
||||||
|
while let Some(line_end) = line_buffer.find('\n') {
|
||||||
|
let line = line_buffer[..line_end].trim().to_string();
|
||||||
|
line_buffer = line_buffer[line_end + 1..].to_string();
|
||||||
|
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse SSE event line
|
||||||
|
if let Some(data) = line.strip_prefix("data: ") {
|
||||||
|
if let Ok(event) = serde_json::from_str::<SseEvent>(data) {
|
||||||
|
match event.event_type.as_str() {
|
||||||
|
"content_block_start" => {
|
||||||
|
if let Some(ref block) = event.content_block {
|
||||||
|
if block.content_type == "thinking" {
|
||||||
|
in_thinking = true;
|
||||||
|
if !has_reasoning {
|
||||||
|
has_reasoning = true;
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.start_thinking();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"content_block_delta" => {
|
||||||
|
if let Some(ref delta) = event.delta {
|
||||||
|
// Thinking delta - ignore content but track state
|
||||||
|
if delta.thinking.is_some() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Text delta - collect
|
||||||
|
if in_thinking && delta.text.is_some() {
|
||||||
|
// Transition from thinking to text
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
in_thinking = false;
|
||||||
|
}
|
||||||
|
if let Some(ref text) = delta.text
|
||||||
|
&& !text.is_empty()
|
||||||
|
{
|
||||||
|
has_content = true;
|
||||||
|
content_buffer.push_str(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"content_block_stop" => {
|
||||||
|
if in_thinking {
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
in_thinking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure thinking state is ended
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = content_buffer.trim().to_string();
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
if has_reasoning && !has_content {
|
||||||
|
bail!(
|
||||||
|
"Anthropic returned thinking content but no final answer. \
|
||||||
|
The model may have entered an incomplete thinking state. \
|
||||||
|
Please try again or disable thinking mode."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
bail!(
|
||||||
|
"No response from Anthropic. \
|
||||||
|
If thinking mode is enabled, try disabling it or ensure the model supports it."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Available Anthropic models
|
/// Available Anthropic models (Claude 4 series with extended thinking)
|
||||||
pub const ANTHROPIC_MODELS: &[&str] = &[
|
pub const ANTHROPIC_MODELS: &[&str] = &[
|
||||||
|
"claude-opus-4-7",
|
||||||
|
"claude-sonnet-4-6",
|
||||||
|
"claude-haiku-4-5",
|
||||||
|
// Legacy models
|
||||||
"claude-3-opus-20240229",
|
"claude-3-opus-20240229",
|
||||||
"claude-3-sonnet-20240229",
|
"claude-3-sonnet-20240229",
|
||||||
"claude-3-haiku-20240307",
|
"claude-3-haiku-20240307",
|
||||||
@@ -210,7 +579,6 @@ pub const ANTHROPIC_MODELS: &[&str] = &[
|
|||||||
"claude-instant-1.2",
|
"claude-instant-1.2",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Check if a model name is valid
|
|
||||||
pub fn is_valid_model(model: &str) -> bool {
|
pub fn is_valid_model(model: &str) -> bool {
|
||||||
ANTHROPIC_MODELS.contains(&model)
|
ANTHROPIC_MODELS.contains(&model)
|
||||||
}
|
}
|
||||||
@@ -220,8 +588,68 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_model_validation() {
|
fn test_model_validation_claude4() {
|
||||||
|
assert!(is_valid_model("claude-opus-4-7"));
|
||||||
|
assert!(is_valid_model("claude-sonnet-4-6"));
|
||||||
|
assert!(is_valid_model("claude-haiku-4-5"));
|
||||||
assert!(is_valid_model("claude-3-sonnet-20240229"));
|
assert!(is_valid_model("claude-3-sonnet-20240229"));
|
||||||
assert!(!is_valid_model("invalid-model"));
|
assert!(!is_valid_model("invalid-model"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_config_serialization() {
|
||||||
|
let config = ThinkingConfig {
|
||||||
|
thinking_type: "enabled".to_string(),
|
||||||
|
budget_tokens: Some(2048),
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&config).unwrap();
|
||||||
|
assert!(json.contains(r#""type":"enabled""#));
|
||||||
|
assert!(json.contains(r#""budget_tokens":2048"#));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_config_disabled_serialization() {
|
||||||
|
let config = ThinkingConfig {
|
||||||
|
thinking_type: "disabled".to_string(),
|
||||||
|
budget_tokens: None,
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&config).unwrap();
|
||||||
|
assert_eq!(json, r#"{"type":"disabled"}"#);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_system_content_serialization() {
|
||||||
|
let content = SystemContent {
|
||||||
|
content_type: "text".to_string(),
|
||||||
|
text: "You are helpful.".to_string(),
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&content).unwrap();
|
||||||
|
assert!(json.contains(r#""type":"text""#));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sse_event_parsing_content_block_start() {
|
||||||
|
let json = r#"{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}"#;
|
||||||
|
let event: SseEvent = serde_json::from_str(json).unwrap();
|
||||||
|
assert_eq!(event.event_type, "content_block_start");
|
||||||
|
assert_eq!(event.content_block.unwrap().content_type, "thinking");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sse_event_parsing_text_delta() {
|
||||||
|
let json = r#"{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}"#;
|
||||||
|
let event: SseEvent = serde_json::from_str(json).unwrap();
|
||||||
|
assert_eq!(event.event_type, "content_block_delta");
|
||||||
|
assert_eq!(event.delta.unwrap().text, Some("Hello".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_anthropic_content_text() {
|
||||||
|
let msg = AnthropicMessage {
|
||||||
|
role: "user".to_string(),
|
||||||
|
content: AnthropicContent::Text("Hello".to_string()),
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&msg).unwrap();
|
||||||
|
assert!(json.contains(r#""content":"Hello""#));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::thinking::ThinkingStateManager;
|
||||||
use anyhow::{bail, Context, Result};
|
use super::{LlmProvider, create_http_client};
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// DeepSeek API client
|
/// DeepSeek API client
|
||||||
@@ -10,6 +12,11 @@ pub struct DeepSeekClient {
|
|||||||
api_key: String,
|
api_key: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
thinking_enabled: bool,
|
||||||
|
reasoning_effort: Option<String>,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
thinking_state: Option<Arc<ThinkingStateManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -20,13 +27,31 @@ struct ChatCompletionRequest {
|
|||||||
max_tokens: Option<u32>,
|
max_tokens: Option<u32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
temperature: Option<f32>,
|
temperature: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
top_p: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
presence_penalty: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
frequency_penalty: Option<f32>,
|
||||||
stream: bool,
|
stream: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
thinking: Option<ThinkingConfig>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
reasoning_effort: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize)]
|
||||||
|
struct ThinkingConfig {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
thinking_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
struct Message {
|
struct Message {
|
||||||
role: String,
|
role: String,
|
||||||
content: String,
|
content: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -37,6 +62,31 @@ struct ChatCompletionResponse {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Choice {
|
struct Choice {
|
||||||
message: Message,
|
message: Message,
|
||||||
|
#[serde(default)]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Streaming response structures ---
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChunk {
|
||||||
|
choices: Vec<StreamChoice>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChoice {
|
||||||
|
delta: StreamDelta,
|
||||||
|
#[serde(default)]
|
||||||
|
finish_reason: Option<String>,
|
||||||
|
index: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Default)]
|
||||||
|
struct StreamDelta {
|
||||||
|
#[serde(default)]
|
||||||
|
content: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -52,55 +102,114 @@ struct ApiError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DeepSeekClient {
|
impl DeepSeekClient {
|
||||||
/// Create new DeepSeek client
|
|
||||||
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(300))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base_url: "https://api.deepseek.com/v1".to_string(),
|
base_url: "https://api.deepseek.com".to_string(),
|
||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
reasoning_effort: None,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create with custom base URL
|
|
||||||
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
|
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(300))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base_url: base_url.trim_end_matches('/').to_string(),
|
base_url: base_url.trim_end_matches('/').to_string(),
|
||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
reasoning_effort: None,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout
|
|
||||||
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
||||||
self.client = create_http_client(timeout)?;
|
self.client = create_http_client(timeout)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate API key
|
pub fn with_thinking(mut self, enabled: bool) -> Self {
|
||||||
pub async fn validate_key(&self) -> Result<bool> {
|
self.thinking_enabled = enabled;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_reasoning_effort(mut self, effort: Option<String>) -> Self {
|
||||||
|
self.reasoning_effort = effort;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_thinking_state(mut self, state: Arc<ThinkingStateManager>) -> Self {
|
||||||
|
self.thinking_state = Some(state);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
let url = format!("{}/models", self.base_url);
|
let url = format!("{}/models", self.base_url);
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("Failed to validate DeepSeek API key")?;
|
.context("Failed to list DeepSeek models")?;
|
||||||
|
|
||||||
if response.status().is_success() {
|
if !response.status().is_success() {
|
||||||
Ok(true)
|
|
||||||
} else if response.status().as_u16() == 401 {
|
|
||||||
Ok(false)
|
|
||||||
} else {
|
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
bail!("DeepSeek API error: {} - {}", status, text)
|
bail!("DeepSeek API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ModelsResponse {
|
||||||
|
data: Vec<ModelId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ModelId {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let result: ModelsResponse = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.context("Failed to parse DeepSeek response")?;
|
||||||
|
|
||||||
|
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn validate_key(&self) -> Result<bool> {
|
||||||
|
match self.list_models().await {
|
||||||
|
Ok(_) => Ok(true),
|
||||||
|
Err(e) => {
|
||||||
|
let err_str = e.to_string();
|
||||||
|
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||||
|
Ok(false)
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,14 +217,13 @@ impl DeepSeekClient {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LlmProvider for DeepSeekClient {
|
impl LlmProvider for DeepSeekClient {
|
||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![
|
let messages = vec![Message {
|
||||||
Message {
|
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: prompt.to_string(),
|
||||||
},
|
reasoning_content: None,
|
||||||
];
|
}];
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
||||||
@@ -125,15 +233,17 @@ impl LlmProvider for DeepSeekClient {
|
|||||||
messages.push(Message {
|
messages.push(Message {
|
||||||
role: "system".to_string(),
|
role: "system".to_string(),
|
||||||
content: system.to_string(),
|
content: system.to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
messages.push(Message {
|
messages.push(Message {
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: user.to_string(),
|
content: user.to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_available(&self) -> bool {
|
async fn is_available(&self) -> bool {
|
||||||
@@ -146,22 +256,102 @@ impl LlmProvider for DeepSeekClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DeepSeekClient {
|
impl DeepSeekClient {
|
||||||
|
async fn chat_completion_with_retry(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
|
let mut last_error = None;
|
||||||
|
|
||||||
|
for attempt in 1..=3 {
|
||||||
|
match self.chat_completion(messages.clone()).await {
|
||||||
|
Ok(result) => return Ok(result),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = e.to_string();
|
||||||
|
// 网络临时错误才重试
|
||||||
|
let is_retryable = err_msg.contains("timeout")
|
||||||
|
|| err_msg.contains("connection")
|
||||||
|
|| err_msg.contains("temporary")
|
||||||
|
|| err_msg.contains("5")
|
||||||
|
&& (err_msg.contains("500")
|
||||||
|
|| err_msg.contains("502")
|
||||||
|
|| err_msg.contains("503")
|
||||||
|
|| err_msg.contains("504"));
|
||||||
|
|
||||||
|
if !is_retryable || attempt == 3 {
|
||||||
|
last_error = Some(e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 指数退避
|
||||||
|
tokio::time::sleep(Duration::from_millis(500 * 2u64.pow(attempt - 1))).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Request failed after retries")))
|
||||||
|
}
|
||||||
|
|
||||||
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
let url = format!("{}/chat/completions", self.base_url);
|
let url = format!("{}/chat/completions", self.base_url);
|
||||||
|
|
||||||
let request = ChatCompletionRequest {
|
let thinking = Some(ThinkingConfig {
|
||||||
model: self.model.clone(),
|
thinking_type: if self.thinking_enabled {
|
||||||
messages,
|
"enabled".to_string()
|
||||||
max_tokens: Some(500),
|
} else {
|
||||||
temperature: Some(0.7),
|
"disabled".to_string()
|
||||||
stream: false,
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 思考模式下,temperature/top_p 等参数不应传递
|
||||||
|
// 非思考模式下可以正常传递
|
||||||
|
let (temperature, max_tokens, top_p, presence_penalty, frequency_penalty) =
|
||||||
|
if self.thinking_enabled {
|
||||||
|
(None, Some(self.max_tokens), None, None, None)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
Some(self.temperature),
|
||||||
|
Some(self.max_tokens),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let reasoning_effort = if self.thinking_enabled {
|
||||||
.post(&url)
|
self.reasoning_effort.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let request = ChatCompletionRequest {
|
||||||
|
model: self.model.clone(),
|
||||||
|
messages: messages.clone(),
|
||||||
|
max_tokens,
|
||||||
|
temperature,
|
||||||
|
top_p,
|
||||||
|
presence_penalty,
|
||||||
|
frequency_penalty,
|
||||||
|
stream: self.thinking_enabled,
|
||||||
|
thinking,
|
||||||
|
reasoning_effort,
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.thinking_enabled {
|
||||||
|
self.streaming_chat_completion(&url, &request).await
|
||||||
|
} else {
|
||||||
|
self.non_streaming_chat_completion(&url, &request).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 非流式请求(非思考模式)
|
||||||
|
async fn non_streaming_chat_completion(
|
||||||
|
&self,
|
||||||
|
url: &str,
|
||||||
|
request: &ChatCompletionRequest,
|
||||||
|
) -> Result<String> {
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.json(&request)
|
.json(request)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("Failed to send request to DeepSeek")?;
|
.context("Failed to send request to DeepSeek")?;
|
||||||
@@ -171,9 +361,12 @@ impl DeepSeekClient {
|
|||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
// Try to parse error
|
|
||||||
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
bail!("DeepSeek API error: {} ({})", error.error.message, error.error.error_type);
|
bail!(
|
||||||
|
"DeepSeek API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("DeepSeek API error: {} - {}", status, text);
|
bail!("DeepSeek API error: {} - {}", status, text);
|
||||||
@@ -184,21 +377,170 @@ impl DeepSeekClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse DeepSeek response")?;
|
.context("Failed to parse DeepSeek response")?;
|
||||||
|
|
||||||
result.choices
|
result
|
||||||
|
.choices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.map(|c| c.message.content.trim().to_string())
|
.map(|c| c.message.content.trim().to_string())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No response from DeepSeek"))
|
.ok_or_else(|| anyhow::anyhow!("No response from DeepSeek"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 流式请求(思考模式),处理 reasoning_content 和 content
|
||||||
|
async fn streaming_chat_completion(
|
||||||
|
&self,
|
||||||
|
url: &str,
|
||||||
|
request: &ChatCompletionRequest,
|
||||||
|
) -> Result<String> {
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(url)
|
||||||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "text/event-stream")
|
||||||
|
.json(request)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send streaming request to DeepSeek")?;
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
|
bail!(
|
||||||
|
"DeepSeek API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("DeepSeek API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut content_buffer = String::new();
|
||||||
|
let mut has_reasoning = false;
|
||||||
|
let mut has_content = false;
|
||||||
|
let mut stream_ended = false;
|
||||||
|
|
||||||
|
let thinking_state = self.thinking_state.as_ref();
|
||||||
|
|
||||||
|
let mut byte_stream = response.bytes_stream();
|
||||||
|
let mut line_buffer = String::new();
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
|
while let Some(chunk) = byte_stream.next().await {
|
||||||
|
let chunk = chunk.context("Failed to read streaming response chunk")?;
|
||||||
|
let chunk_str =
|
||||||
|
String::from_utf8(chunk.to_vec()).context("Invalid UTF-8 in stream chunk")?;
|
||||||
|
|
||||||
|
line_buffer.push_str(&chunk_str);
|
||||||
|
|
||||||
|
// 处理完整行
|
||||||
|
while let Some(line_end) = line_buffer.find('\n') {
|
||||||
|
let line = line_buffer[..line_end].trim().to_string();
|
||||||
|
line_buffer = line_buffer[line_end + 1..].to_string();
|
||||||
|
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSE 格式:data: {...} 或 data: [DONE]
|
||||||
|
if line == "data: [DONE]" {
|
||||||
|
stream_ended = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(json_str) = line.strip_prefix("data: ") {
|
||||||
|
match serde_json::from_str::<StreamChunk>(json_str) {
|
||||||
|
Ok(chunk) => {
|
||||||
|
for choice in &chunk.choices {
|
||||||
|
// 处理 reasoning_content
|
||||||
|
if let Some(ref reasoning) = choice.delta.reasoning_content
|
||||||
|
&& !reasoning.is_empty()
|
||||||
|
{
|
||||||
|
if !has_reasoning {
|
||||||
|
has_reasoning = true;
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.start_thinking();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// reasoning_content 不对外输出,仅用于内部状态判断
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 content
|
||||||
|
if let Some(ref content) = choice.delta.content
|
||||||
|
&& !content.is_empty()
|
||||||
|
{
|
||||||
|
// reasoning 结束,content 开始出现时移除 thinking 标识
|
||||||
|
if has_reasoning
|
||||||
|
&& !has_content
|
||||||
|
&& let Some(state) = thinking_state
|
||||||
|
{
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
has_content = true;
|
||||||
|
content_buffer.push_str(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查 finish_reason
|
||||||
|
if let Some(ref reason) = choice.finish_reason
|
||||||
|
&& reason == "stop"
|
||||||
|
{
|
||||||
|
stream_ended = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// 忽略无法解析的行(可能是心跳或注释)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if stream_ended {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保思考状态已结束
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = content_buffer.trim().to_string();
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
if has_reasoning && !has_content {
|
||||||
|
bail!(
|
||||||
|
"DeepSeek returned reasoning content but no final answer. \
|
||||||
|
The model may have entered an incomplete thinking state. \
|
||||||
|
Please try again or disable thinking mode."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
bail!(
|
||||||
|
"No response from DeepSeek. \
|
||||||
|
If thinking mode is enabled, try disabling it or ensure the model supports it."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Available DeepSeek models
|
/// 可用 DeepSeek 模型列表
|
||||||
|
/// deepseek-chat / deepseek-reasoner 将于 2026-07-24 停用,推荐使用 V4 系列
|
||||||
pub const DEEPSEEK_MODELS: &[&str] = &[
|
pub const DEEPSEEK_MODELS: &[&str] = &[
|
||||||
|
"deepseek-v4-flash",
|
||||||
|
"deepseek-v4-pro",
|
||||||
|
// 兼容旧版模型 ID(将于 2026-07-24 停用)
|
||||||
"deepseek-chat",
|
"deepseek-chat",
|
||||||
"deepseek-coder",
|
"deepseek-reasoner",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Check if a model name is valid
|
|
||||||
pub fn is_valid_model(model: &str) -> bool {
|
pub fn is_valid_model(model: &str) -> bool {
|
||||||
DEEPSEEK_MODELS.contains(&model)
|
DEEPSEEK_MODELS.contains(&model)
|
||||||
}
|
}
|
||||||
@@ -208,8 +550,73 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_model_validation() {
|
fn test_model_validation_v4() {
|
||||||
|
assert!(is_valid_model("deepseek-v4-flash"));
|
||||||
|
assert!(is_valid_model("deepseek-v4-pro"));
|
||||||
assert!(is_valid_model("deepseek-chat"));
|
assert!(is_valid_model("deepseek-chat"));
|
||||||
|
assert!(is_valid_model("deepseek-reasoner"));
|
||||||
assert!(!is_valid_model("invalid-model"));
|
assert!(!is_valid_model("invalid-model"));
|
||||||
|
assert!(!is_valid_model("deepseek-v3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_builder_defaults() {
|
||||||
|
let client = DeepSeekClient::new("test-key", "deepseek-v4-flash").unwrap();
|
||||||
|
assert!(!client.thinking_enabled);
|
||||||
|
assert_eq!(client.max_tokens, 500);
|
||||||
|
assert_eq!(client.temperature, 0.7);
|
||||||
|
assert!(client.reasoning_effort.is_none());
|
||||||
|
assert!(client.thinking_state.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_builder_with_thinking() {
|
||||||
|
let client = DeepSeekClient::new("test-key", "deepseek-v4-flash")
|
||||||
|
.unwrap()
|
||||||
|
.with_thinking(true)
|
||||||
|
.with_reasoning_effort(Some("high".to_string()))
|
||||||
|
.with_max_tokens(1000)
|
||||||
|
.with_temperature(0.5);
|
||||||
|
|
||||||
|
assert!(client.thinking_enabled);
|
||||||
|
assert_eq!(client.reasoning_effort, Some("high".to_string()));
|
||||||
|
assert_eq!(client.max_tokens, 1000);
|
||||||
|
assert_eq!(client.temperature, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_config_serialization() {
|
||||||
|
let config = ThinkingConfig {
|
||||||
|
thinking_type: "enabled".to_string(),
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&config).unwrap();
|
||||||
|
assert_eq!(json, r#"{"type":"enabled"}"#);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_message_serialization_without_reasoning() {
|
||||||
|
let msg = Message {
|
||||||
|
role: "user".to_string(),
|
||||||
|
content: "Hello".to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&msg).unwrap();
|
||||||
|
assert!(!json.contains("reasoning_content"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_stream_delta_parsing() {
|
||||||
|
let json = r#"{"content":"Hello","reasoning_content":null}"#;
|
||||||
|
let delta: StreamDelta = serde_json::from_str(json).unwrap();
|
||||||
|
assert_eq!(delta.content, Some("Hello".to_string()));
|
||||||
|
assert!(delta.reasoning_content.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_stream_delta_reasoning_only() {
|
||||||
|
let json = r#"{"content":null,"reasoning_content":"Let me think..."}"#;
|
||||||
|
let delta: StreamDelta = serde_json::from_str(json).unwrap();
|
||||||
|
assert!(delta.content.is_none());
|
||||||
|
assert_eq!(delta.reasoning_content, Some("Let me think...".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
451
src/llm/kimi.rs
451
src/llm/kimi.rs
@@ -1,7 +1,9 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::thinking::ThinkingStateManager;
|
||||||
use anyhow::{bail, Context, Result};
|
use super::{LlmProvider, create_http_client};
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Kimi API client (Moonshot AI)
|
/// Kimi API client (Moonshot AI)
|
||||||
@@ -10,6 +12,10 @@ pub struct KimiClient {
|
|||||||
api_key: String,
|
api_key: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
thinking_enabled: bool,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
thinking_state: Option<Arc<ThinkingStateManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -21,12 +27,22 @@ struct ChatCompletionRequest {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
temperature: Option<f32>,
|
temperature: Option<f32>,
|
||||||
stream: bool,
|
stream: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
thinking: Option<ThinkingConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize)]
|
||||||
|
struct ThinkingConfig {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
thinking_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
struct Message {
|
struct Message {
|
||||||
role: String,
|
role: String,
|
||||||
content: String,
|
content: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -37,6 +53,31 @@ struct ChatCompletionResponse {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Choice {
|
struct Choice {
|
||||||
message: Message,
|
message: Message,
|
||||||
|
#[serde(default)]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Streaming response structures ---
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChunk {
|
||||||
|
choices: Vec<StreamChoice>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChoice {
|
||||||
|
delta: StreamDelta,
|
||||||
|
#[serde(default)]
|
||||||
|
finish_reason: Option<String>,
|
||||||
|
index: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Default)]
|
||||||
|
struct StreamDelta {
|
||||||
|
#[serde(default)]
|
||||||
|
content: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -52,55 +93,107 @@ struct ApiError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl KimiClient {
|
impl KimiClient {
|
||||||
/// Create new Kimi client
|
|
||||||
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
pub fn new(api_key: &str, model: &str) -> Result<Self> {
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(300))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base_url: "https://api.moonshot.cn/v1".to_string(),
|
base_url: "https://api.moonshot.cn/v1".to_string(),
|
||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 1.0,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create with custom base URL
|
|
||||||
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
|
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(300))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base_url: base_url.trim_end_matches('/').to_string(),
|
base_url: base_url.trim_end_matches('/').to_string(),
|
||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 1.0,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout
|
|
||||||
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
||||||
self.client = create_http_client(timeout)?;
|
self.client = create_http_client(timeout)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate API key
|
pub fn with_thinking(mut self, enabled: bool) -> Self {
|
||||||
pub async fn validate_key(&self) -> Result<bool> {
|
self.thinking_enabled = enabled;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_thinking_state(mut self, state: Arc<ThinkingStateManager>) -> Self {
|
||||||
|
self.thinking_state = Some(state);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
let url = format!("{}/models", self.base_url);
|
let url = format!("{}/models", self.base_url);
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("Failed to validate Kimi API key")?;
|
.context("Failed to list Kimi models")?;
|
||||||
|
|
||||||
if response.status().is_success() {
|
if !response.status().is_success() {
|
||||||
Ok(true)
|
|
||||||
} else if response.status().as_u16() == 401 {
|
|
||||||
Ok(false)
|
|
||||||
} else {
|
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
bail!("Kimi API error: {} - {}", status, text)
|
bail!("Kimi API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ModelsResponse {
|
||||||
|
data: Vec<ModelId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ModelId {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let result: ModelsResponse = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.context("Failed to parse Kimi response")?;
|
||||||
|
|
||||||
|
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn validate_key(&self) -> Result<bool> {
|
||||||
|
match self.list_models().await {
|
||||||
|
Ok(_) => Ok(true),
|
||||||
|
Err(e) => {
|
||||||
|
let err_str = e.to_string();
|
||||||
|
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||||
|
Ok(false)
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,14 +201,13 @@ impl KimiClient {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LlmProvider for KimiClient {
|
impl LlmProvider for KimiClient {
|
||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![
|
let messages = vec![Message {
|
||||||
Message {
|
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: prompt.to_string(),
|
||||||
},
|
reasoning_content: None,
|
||||||
];
|
}];
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
||||||
@@ -125,15 +217,17 @@ impl LlmProvider for KimiClient {
|
|||||||
messages.push(Message {
|
messages.push(Message {
|
||||||
role: "system".to_string(),
|
role: "system".to_string(),
|
||||||
content: system.to_string(),
|
content: system.to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
messages.push(Message {
|
messages.push(Message {
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: user.to_string(),
|
content: user.to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_available(&self) -> bool {
|
async fn is_available(&self) -> bool {
|
||||||
@@ -146,22 +240,84 @@ impl LlmProvider for KimiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl KimiClient {
|
impl KimiClient {
|
||||||
|
async fn chat_completion_with_retry(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
|
let mut last_error = None;
|
||||||
|
|
||||||
|
for attempt in 1..=3 {
|
||||||
|
match self.chat_completion(messages.clone()).await {
|
||||||
|
Ok(result) => return Ok(result),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = e.to_string();
|
||||||
|
let is_retryable = err_msg.contains("timeout")
|
||||||
|
|| err_msg.contains("connection")
|
||||||
|
|| err_msg.contains("temporary")
|
||||||
|
|| err_msg.contains("5")
|
||||||
|
&& (err_msg.contains("500")
|
||||||
|
|| err_msg.contains("502")
|
||||||
|
|| err_msg.contains("503")
|
||||||
|
|| err_msg.contains("504"));
|
||||||
|
|
||||||
|
if !is_retryable || attempt == 3 {
|
||||||
|
last_error = Some(e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(500 * 2u64.pow(attempt - 1))).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Request failed after retries")))
|
||||||
|
}
|
||||||
|
|
||||||
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
let url = format!("{}/chat/completions", self.base_url);
|
let url = format!("{}/chat/completions", self.base_url);
|
||||||
|
|
||||||
let request = ChatCompletionRequest {
|
let thinking = Some(ThinkingConfig {
|
||||||
model: self.model.clone(),
|
thinking_type: if self.thinking_enabled {
|
||||||
messages,
|
"enabled".to_string()
|
||||||
max_tokens: Some(500),
|
} else {
|
||||||
temperature: Some(0.7),
|
"disabled".to_string()
|
||||||
stream: false,
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Kimi API temperature 要求:
|
||||||
|
// - 思考模式: temperature 必须为 1.0
|
||||||
|
// - 非思考模式: temperature 必须为 0.6
|
||||||
|
let temperature = if self.thinking_enabled {
|
||||||
|
Some(1.0)
|
||||||
|
} else {
|
||||||
|
Some(0.6)
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let request = ChatCompletionRequest {
|
||||||
.post(&url)
|
model: self.model.clone(),
|
||||||
|
messages: messages.clone(),
|
||||||
|
max_tokens: Some(self.max_tokens),
|
||||||
|
temperature,
|
||||||
|
stream: self.thinking_enabled,
|
||||||
|
thinking,
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.thinking_enabled {
|
||||||
|
self.streaming_chat_completion(&url, &request).await
|
||||||
|
} else {
|
||||||
|
self.non_streaming_chat_completion(&url, &request).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 非流式请求(非思考模式)
|
||||||
|
async fn non_streaming_chat_completion(
|
||||||
|
&self,
|
||||||
|
url: &str,
|
||||||
|
request: &ChatCompletionRequest,
|
||||||
|
) -> Result<String> {
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.json(&request)
|
.json(request)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("Failed to send request to Kimi")?;
|
.context("Failed to send request to Kimi")?;
|
||||||
@@ -171,9 +327,12 @@ impl KimiClient {
|
|||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
// Try to parse error
|
|
||||||
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
bail!("Kimi API error: {} ({})", error.error.message, error.error.error_type);
|
bail!(
|
||||||
|
"Kimi API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("Kimi API error: {} - {}", status, text);
|
bail!("Kimi API error: {} - {}", status, text);
|
||||||
@@ -184,22 +343,178 @@ impl KimiClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse Kimi response")?;
|
.context("Failed to parse Kimi response")?;
|
||||||
|
|
||||||
result.choices
|
result
|
||||||
|
.choices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.map(|c| c.message.content.trim().to_string())
|
.map(|c| {
|
||||||
|
let content = c.message.content.trim().to_string();
|
||||||
|
if content.is_empty() {
|
||||||
|
c.reasoning_content
|
||||||
|
.or(c.message.reasoning_content)
|
||||||
|
.map(|r| r.trim().to_string())
|
||||||
|
.unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
content
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No response from Kimi"))
|
.ok_or_else(|| anyhow::anyhow!("No response from Kimi"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 流式请求(思考模式),处理 reasoning_content 和 content
|
||||||
|
async fn streaming_chat_completion(
|
||||||
|
&self,
|
||||||
|
url: &str,
|
||||||
|
request: &ChatCompletionRequest,
|
||||||
|
) -> Result<String> {
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(url)
|
||||||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "text/event-stream")
|
||||||
|
.json(request)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send streaming request to Kimi")?;
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
|
bail!(
|
||||||
|
"Kimi API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("Kimi API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut content_buffer = String::new();
|
||||||
|
let mut has_reasoning = false;
|
||||||
|
let mut has_content = false;
|
||||||
|
let mut stream_ended = false;
|
||||||
|
|
||||||
|
let thinking_state = self.thinking_state.as_ref();
|
||||||
|
|
||||||
|
let mut byte_stream = response.bytes_stream();
|
||||||
|
let mut line_buffer = String::new();
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
|
while let Some(chunk) = byte_stream.next().await {
|
||||||
|
let chunk = chunk.context("Failed to read streaming response chunk")?;
|
||||||
|
let chunk_str =
|
||||||
|
String::from_utf8(chunk.to_vec()).context("Invalid UTF-8 in stream chunk")?;
|
||||||
|
|
||||||
|
line_buffer.push_str(&chunk_str);
|
||||||
|
|
||||||
|
while let Some(line_end) = line_buffer.find('\n') {
|
||||||
|
let line = line_buffer[..line_end].trim().to_string();
|
||||||
|
line_buffer = line_buffer[line_end + 1..].to_string();
|
||||||
|
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if line == "data: [DONE]" {
|
||||||
|
stream_ended = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(json_str) = line.strip_prefix("data: ") {
|
||||||
|
match serde_json::from_str::<StreamChunk>(json_str) {
|
||||||
|
Ok(chunk) => {
|
||||||
|
for choice in &chunk.choices {
|
||||||
|
if let Some(ref reasoning) = choice.delta.reasoning_content
|
||||||
|
&& !reasoning.is_empty()
|
||||||
|
{
|
||||||
|
if !has_reasoning {
|
||||||
|
has_reasoning = true;
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.start_thinking();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref content) = choice.delta.content
|
||||||
|
&& !content.is_empty()
|
||||||
|
{
|
||||||
|
if has_reasoning
|
||||||
|
&& !has_content
|
||||||
|
&& let Some(state) = thinking_state
|
||||||
|
{
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
has_content = true;
|
||||||
|
content_buffer.push_str(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref reason) = choice.finish_reason
|
||||||
|
&& reason == "stop"
|
||||||
|
{
|
||||||
|
stream_ended = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// 忽略无法解析的行
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if stream_ended {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保思考状态已结束
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = content_buffer.trim().to_string();
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
if has_reasoning && !has_content {
|
||||||
|
bail!(
|
||||||
|
"Kimi returned reasoning content but no final answer. \
|
||||||
|
The model may have entered an incomplete thinking state. \
|
||||||
|
Please try again or disable thinking mode."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
bail!(
|
||||||
|
"No response from Kimi. \
|
||||||
|
If thinking mode is enabled, try disabling it or ensure the model supports it."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Available Kimi models
|
/// 可用 Kimi 模型列表
|
||||||
pub const KIMI_MODELS: &[&str] = &[
|
pub const KIMI_MODELS: &[&str] = &[
|
||||||
|
// K2 系列(推荐)
|
||||||
|
"kimi-k2.6",
|
||||||
|
"kimi-k2.5",
|
||||||
|
"kimi-k2-thinking",
|
||||||
|
"kimi-k2-thinking-turbo",
|
||||||
|
"kimi-k2-instruct",
|
||||||
|
"kimi-k2-instruct-0905",
|
||||||
|
// 兼容旧版模型 ID
|
||||||
"moonshot-v1-8k",
|
"moonshot-v1-8k",
|
||||||
"moonshot-v1-32k",
|
"moonshot-v1-32k",
|
||||||
"moonshot-v1-128k",
|
"moonshot-v1-128k",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Check if a model name is valid
|
|
||||||
pub fn is_valid_model(model: &str) -> bool {
|
pub fn is_valid_model(model: &str) -> bool {
|
||||||
KIMI_MODELS.contains(&model)
|
KIMI_MODELS.contains(&model)
|
||||||
}
|
}
|
||||||
@@ -209,8 +524,64 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_model_validation() {
|
fn test_model_validation_k2() {
|
||||||
|
assert!(is_valid_model("kimi-k2.6"));
|
||||||
|
assert!(is_valid_model("kimi-k2.5"));
|
||||||
|
assert!(is_valid_model("kimi-k2-thinking"));
|
||||||
|
assert!(is_valid_model("kimi-k2-thinking-turbo"));
|
||||||
assert!(is_valid_model("moonshot-v1-8k"));
|
assert!(is_valid_model("moonshot-v1-8k"));
|
||||||
|
assert!(is_valid_model("moonshot-v1-32k"));
|
||||||
|
assert!(is_valid_model("moonshot-v1-128k"));
|
||||||
assert!(!is_valid_model("invalid-model"));
|
assert!(!is_valid_model("invalid-model"));
|
||||||
|
assert!(!is_valid_model("kimi-k1.5"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_builder_defaults() {
|
||||||
|
let client = KimiClient::new("test-key", "kimi-k2.6").unwrap();
|
||||||
|
assert!(!client.thinking_enabled);
|
||||||
|
assert_eq!(client.max_tokens, 500);
|
||||||
|
assert_eq!(client.temperature, 1.0);
|
||||||
|
assert!(client.thinking_state.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_builder_with_thinking() {
|
||||||
|
let client = KimiClient::new("test-key", "kimi-k2.6")
|
||||||
|
.unwrap()
|
||||||
|
.with_thinking(true)
|
||||||
|
.with_max_tokens(1000)
|
||||||
|
.with_temperature(0.5);
|
||||||
|
|
||||||
|
assert!(client.thinking_enabled);
|
||||||
|
assert_eq!(client.max_tokens, 1000);
|
||||||
|
assert_eq!(client.temperature, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_config_serialization() {
|
||||||
|
let config = ThinkingConfig {
|
||||||
|
thinking_type: "enabled".to_string(),
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&config).unwrap();
|
||||||
|
assert_eq!(json, r#"{"type":"enabled"}"#);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_new_defaults() {
|
||||||
|
let client = KimiClient::new("test-key", "kimi-k2.6").unwrap();
|
||||||
|
assert_eq!(client.name(), "kimi");
|
||||||
|
assert!(!client.thinking_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_message_serialization() {
|
||||||
|
let msg = Message {
|
||||||
|
role: "user".to_string(),
|
||||||
|
content: "Hello".to_string(),
|
||||||
|
reasoning_content: None,
|
||||||
|
};
|
||||||
|
let json = serde_json::to_string(&msg).unwrap();
|
||||||
|
assert!(!json.contains("reasoning_content"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
891
src/llm/mod.rs
891
src/llm/mod.rs
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::{LlmProvider, create_http_client};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -9,6 +9,9 @@ pub struct OllamaClient {
|
|||||||
base_url: String,
|
base_url: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
top_p: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -47,20 +50,37 @@ struct ModelInfo {
|
|||||||
impl OllamaClient {
|
impl OllamaClient {
|
||||||
/// Create new Ollama client
|
/// Create new Ollama client
|
||||||
pub fn new(base_url: &str, model: &str) -> Self {
|
pub fn new(base_url: &str, model: &str) -> Self {
|
||||||
let client = create_http_client(Duration::from_secs(120))
|
let client =
|
||||||
.expect("Failed to create HTTP client");
|
create_http_client(Duration::from_secs(120)).expect("Failed to create HTTP client");
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
base_url: base_url.trim_end_matches('/').to_string(),
|
base_url: base_url.trim_end_matches('/').to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout
|
/// Set timeout
|
||||||
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
||||||
self.client = create_http_client(timeout)
|
self.client = create_http_client(timeout).expect("Failed to create HTTP client");
|
||||||
.expect("Failed to create HTTP client");
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_top_p(mut self, top_p: f32) -> Self {
|
||||||
|
self.top_p = Some(top_p);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +88,8 @@ impl OllamaClient {
|
|||||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
let url = format!("{}/api/tags", self.base_url);
|
let url = format!("{}/api/tags", self.base_url);
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
@@ -97,7 +118,8 @@ impl OllamaClient {
|
|||||||
"stream": false,
|
"stream": false,
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.json(&request)
|
.json(&request)
|
||||||
.send()
|
.send()
|
||||||
@@ -143,12 +165,13 @@ impl LlmProvider for OllamaClient {
|
|||||||
system,
|
system,
|
||||||
stream: false,
|
stream: false,
|
||||||
options: GenerationOptions {
|
options: GenerationOptions {
|
||||||
temperature: Some(0.7),
|
temperature: Some(self.temperature),
|
||||||
num_predict: Some(500),
|
num_predict: Some(self.max_tokens),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.json(&request)
|
.json(&request)
|
||||||
.send()
|
.send()
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::thinking::ThinkingStateManager;
|
||||||
use anyhow::{bail, Context, Result};
|
use super::{LlmProvider, create_http_client};
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// OpenAI API client
|
/// OpenAI API client with o-series reasoning support
|
||||||
pub struct OpenAiClient {
|
pub struct OpenAiClient {
|
||||||
base_url: String,
|
base_url: String,
|
||||||
api_key: String,
|
api_key: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
thinking_enabled: bool,
|
||||||
|
reasoning_effort: Option<String>,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
top_p: Option<f32>,
|
||||||
|
thinking_state: Option<Arc<ThinkingStateManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -20,10 +28,14 @@ struct ChatCompletionRequest {
|
|||||||
max_tokens: Option<u32>,
|
max_tokens: Option<u32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
temperature: Option<f32>,
|
temperature: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
top_p: Option<f32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
reasoning_effort: Option<String>,
|
||||||
stream: bool,
|
stream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
struct Message {
|
struct Message {
|
||||||
role: String,
|
role: String,
|
||||||
content: String,
|
content: String,
|
||||||
@@ -39,6 +51,28 @@ struct Choice {
|
|||||||
message: Message,
|
message: Message,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Streaming response structures ---
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChunk {
|
||||||
|
choices: Vec<StreamChoice>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct StreamChoice {
|
||||||
|
delta: StreamDelta,
|
||||||
|
#[serde(default)]
|
||||||
|
finish_reason: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Default)]
|
||||||
|
struct StreamDelta {
|
||||||
|
#[serde(default)]
|
||||||
|
content: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
reasoning_content: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct ErrorResponse {
|
struct ErrorResponse {
|
||||||
error: ApiError,
|
error: ApiError,
|
||||||
@@ -61,20 +95,55 @@ impl OpenAiClient {
|
|||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
reasoning_effort: None,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout
|
|
||||||
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
pub fn with_timeout(mut self, timeout: Duration) -> Result<Self> {
|
||||||
self.client = create_http_client(timeout)?;
|
self.client = create_http_client(timeout)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List available models
|
pub fn with_thinking(mut self, enabled: bool) -> Self {
|
||||||
|
self.thinking_enabled = enabled;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_reasoning_effort(mut self, effort: Option<String>) -> Self {
|
||||||
|
self.reasoning_effort = effort;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_top_p(mut self, top_p: f32) -> Self {
|
||||||
|
self.top_p = Some(top_p);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_thinking_state(mut self, state: Arc<ThinkingStateManager>) -> Self {
|
||||||
|
self.thinking_state = Some(state);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
let url = format!("{}/models", self.base_url);
|
let url = format!("{}/models", self.base_url);
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.send()
|
.send()
|
||||||
@@ -105,7 +174,6 @@ impl OpenAiClient {
|
|||||||
Ok(result.data.into_iter().map(|m| m.id).collect())
|
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate API key
|
|
||||||
pub async fn validate_key(&self) -> Result<bool> {
|
pub async fn validate_key(&self) -> Result<bool> {
|
||||||
match self.list_models().await {
|
match self.list_models().await {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
@@ -124,14 +192,12 @@ impl OpenAiClient {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LlmProvider for OpenAiClient {
|
impl LlmProvider for OpenAiClient {
|
||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![
|
let messages = vec![Message {
|
||||||
Message {
|
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: prompt.to_string(),
|
||||||
},
|
}];
|
||||||
];
|
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
|
||||||
@@ -149,7 +215,7 @@ impl LlmProvider for OpenAiClient {
|
|||||||
content: user.to_string(),
|
content: user.to_string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion_with_retry(messages).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_available(&self) -> bool {
|
async fn is_available(&self) -> bool {
|
||||||
@@ -162,18 +228,63 @@ impl LlmProvider for OpenAiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl OpenAiClient {
|
impl OpenAiClient {
|
||||||
|
async fn chat_completion_with_retry(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
|
let mut last_error = None;
|
||||||
|
|
||||||
|
for attempt in 1..=3 {
|
||||||
|
match self.chat_completion(messages.clone()).await {
|
||||||
|
Ok(result) => return Ok(result),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = e.to_string();
|
||||||
|
let is_retryable = err_msg.contains("timeout")
|
||||||
|
|| err_msg.contains("connection")
|
||||||
|
|| err_msg.contains("temporary")
|
||||||
|
|| err_msg.contains("5")
|
||||||
|
&& (err_msg.contains("500")
|
||||||
|
|| err_msg.contains("502")
|
||||||
|
|| err_msg.contains("503")
|
||||||
|
|| err_msg.contains("504"));
|
||||||
|
|
||||||
|
if !is_retryable || attempt == 3 {
|
||||||
|
last_error = Some(e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(500 * 2u64.pow(attempt - 1))).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Request failed after retries")))
|
||||||
|
}
|
||||||
|
|
||||||
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
|
if self.thinking_enabled {
|
||||||
|
self.streaming_chat_completion(messages).await
|
||||||
|
} else {
|
||||||
|
self.non_streaming_chat_completion(messages).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn non_streaming_chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
let url = format!("{}/chat/completions", self.base_url);
|
let url = format!("{}/chat/completions", self.base_url);
|
||||||
|
|
||||||
let request = ChatCompletionRequest {
|
let request = ChatCompletionRequest {
|
||||||
model: self.model.clone(),
|
model: self.model.clone(),
|
||||||
messages,
|
messages,
|
||||||
max_tokens: Some(500),
|
max_tokens: Some(self.max_tokens),
|
||||||
temperature: Some(0.7),
|
temperature: Some(self.temperature),
|
||||||
|
top_p: self.top_p,
|
||||||
|
reasoning_effort: if is_reasoning_model(&self.model) {
|
||||||
|
Some("none".to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
@@ -187,9 +298,12 @@ impl OpenAiClient {
|
|||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
// Try to parse error
|
|
||||||
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
bail!("OpenAI API error: {} ({})", error.error.message, error.error.error_type);
|
bail!(
|
||||||
|
"OpenAI API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("OpenAI API error: {} - {}", status, text);
|
bail!("OpenAI API error: {} - {}", status, text);
|
||||||
@@ -200,12 +314,144 @@ impl OpenAiClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse OpenAI response")?;
|
.context("Failed to parse OpenAI response")?;
|
||||||
|
|
||||||
result.choices
|
result
|
||||||
|
.choices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.map(|c| c.message.content.trim().to_string())
|
.map(|c| c.message.content.trim().to_string())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No response from OpenAI"))
|
.ok_or_else(|| anyhow::anyhow!("No response from OpenAI"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Streaming request for reasoning mode, filters reasoning_content from output
|
||||||
|
async fn streaming_chat_completion(&self, messages: Vec<Message>) -> Result<String> {
|
||||||
|
let url = format!("{}/chat/completions", self.base_url);
|
||||||
|
|
||||||
|
// For reasoning/thinking mode, omit temperature and top_p
|
||||||
|
let request = ChatCompletionRequest {
|
||||||
|
model: self.model.clone(),
|
||||||
|
messages,
|
||||||
|
max_tokens: Some(self.max_tokens),
|
||||||
|
temperature: None,
|
||||||
|
top_p: None,
|
||||||
|
reasoning_effort: self.reasoning_effort.clone(),
|
||||||
|
stream: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(&url)
|
||||||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "text/event-stream")
|
||||||
|
.json(&request)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send streaming request to OpenAI")?;
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
let text = response.text().await.unwrap_or_default();
|
||||||
|
|
||||||
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
|
bail!(
|
||||||
|
"OpenAI API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("OpenAI API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut content_buffer = String::new();
|
||||||
|
let mut has_reasoning = false;
|
||||||
|
let mut has_content = false;
|
||||||
|
|
||||||
|
let thinking_state = self.thinking_state.as_ref();
|
||||||
|
|
||||||
|
let mut byte_stream = response.bytes_stream();
|
||||||
|
let mut line_buffer = String::new();
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
|
while let Some(chunk) = byte_stream.next().await {
|
||||||
|
let chunk = chunk.context("Failed to read streaming response chunk")?;
|
||||||
|
let chunk_str =
|
||||||
|
String::from_utf8(chunk.to_vec()).context("Invalid UTF-8 in stream chunk")?;
|
||||||
|
|
||||||
|
line_buffer.push_str(&chunk_str);
|
||||||
|
|
||||||
|
while let Some(line_end) = line_buffer.find('\n') {
|
||||||
|
let line = line_buffer[..line_end].trim().to_string();
|
||||||
|
line_buffer = line_buffer[line_end + 1..].to_string();
|
||||||
|
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if line == "data: [DONE]" {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(json_str) = line.strip_prefix("data: ") {
|
||||||
|
if let Ok(chunk) = serde_json::from_str::<StreamChunk>(json_str) {
|
||||||
|
for choice in &chunk.choices {
|
||||||
|
// Handle reasoning_content (o-series)
|
||||||
|
if let Some(ref reasoning) = choice.delta.reasoning_content
|
||||||
|
&& !reasoning.is_empty()
|
||||||
|
{
|
||||||
|
if !has_reasoning {
|
||||||
|
has_reasoning = true;
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.start_thinking();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle content
|
||||||
|
if let Some(ref content) = choice.delta.content
|
||||||
|
&& !content.is_empty()
|
||||||
|
{
|
||||||
|
if has_reasoning
|
||||||
|
&& !has_content
|
||||||
|
&& let Some(state) = thinking_state
|
||||||
|
{
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
has_content = true;
|
||||||
|
content_buffer.push_str(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(state) = thinking_state {
|
||||||
|
state.end_thinking();
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = content_buffer.trim().to_string();
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
if has_reasoning && !has_content {
|
||||||
|
bail!(
|
||||||
|
"OpenAI returned reasoning content but no final answer. \
|
||||||
|
The model may have entered an incomplete reasoning state. \
|
||||||
|
Please try again or disable thinking mode."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
bail!(
|
||||||
|
"No response from OpenAI. \
|
||||||
|
If thinking mode is enabled, try disabling it or ensure the model supports reasoning."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Azure OpenAI client (extends OpenAI with Azure-specific config)
|
/// Azure OpenAI client (extends OpenAI with Azure-specific config)
|
||||||
@@ -215,16 +461,16 @@ pub struct AzureOpenAiClient {
|
|||||||
deployment: String,
|
deployment: String,
|
||||||
api_version: String,
|
api_version: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
thinking_enabled: bool,
|
||||||
|
reasoning_effort: Option<String>,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
top_p: Option<f32>,
|
||||||
|
thinking_state: Option<Arc<ThinkingStateManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AzureOpenAiClient {
|
impl AzureOpenAiClient {
|
||||||
/// Create new Azure OpenAI client
|
pub fn new(endpoint: &str, api_key: &str, deployment: &str, api_version: &str) -> Result<Self> {
|
||||||
pub fn new(
|
|
||||||
endpoint: &str,
|
|
||||||
api_key: &str,
|
|
||||||
deployment: &str,
|
|
||||||
api_version: &str,
|
|
||||||
) -> Result<Self> {
|
|
||||||
let client = create_http_client(Duration::from_secs(60))?;
|
let client = create_http_client(Duration::from_secs(60))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -233,6 +479,12 @@ impl AzureOpenAiClient {
|
|||||||
deployment: deployment.to_string(),
|
deployment: deployment.to_string(),
|
||||||
api_version: api_version.to_string(),
|
api_version: api_version.to_string(),
|
||||||
client,
|
client,
|
||||||
|
thinking_enabled: false,
|
||||||
|
reasoning_effort: None,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
|
thinking_state: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,12 +497,15 @@ impl AzureOpenAiClient {
|
|||||||
let request = ChatCompletionRequest {
|
let request = ChatCompletionRequest {
|
||||||
model: self.deployment.clone(),
|
model: self.deployment.clone(),
|
||||||
messages,
|
messages,
|
||||||
max_tokens: Some(500),
|
max_tokens: Some(self.max_tokens),
|
||||||
temperature: Some(0.7),
|
temperature: Some(self.temperature),
|
||||||
|
top_p: self.top_p,
|
||||||
|
reasoning_effort: self.reasoning_effort.clone(),
|
||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header("api-key", &self.api_key)
|
.header("api-key", &self.api_key)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
@@ -270,10 +525,12 @@ impl AzureOpenAiClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse Azure OpenAI response")?;
|
.context("Failed to parse Azure OpenAI response")?;
|
||||||
|
|
||||||
result.choices
|
result
|
||||||
|
.choices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.map(|c| c.message.content.trim().to_string())
|
.map(|c| c.message.content.trim().to_string())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No response from Azure OpenAI"))
|
.ok_or_else(|| anyhow::anyhow!("No response from Azure OpenAI"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,12 +538,10 @@ impl AzureOpenAiClient {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LlmProvider for AzureOpenAiClient {
|
impl LlmProvider for AzureOpenAiClient {
|
||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![
|
let messages = vec![Message {
|
||||||
Message {
|
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: prompt.to_string(),
|
||||||
},
|
}];
|
||||||
];
|
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion(messages).await
|
||||||
}
|
}
|
||||||
@@ -310,7 +565,6 @@ impl LlmProvider for AzureOpenAiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn is_available(&self) -> bool {
|
async fn is_available(&self) -> bool {
|
||||||
// Simple check - try to make a minimal request
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/openai/deployments/{}/chat/completions?api-version={}",
|
"{}/openai/deployments/{}/chat/completions?api-version={}",
|
||||||
self.endpoint, self.deployment, self.api_version
|
self.endpoint, self.deployment, self.api_version
|
||||||
@@ -324,10 +578,13 @@ impl LlmProvider for AzureOpenAiClient {
|
|||||||
}],
|
}],
|
||||||
max_tokens: Some(5),
|
max_tokens: Some(5),
|
||||||
temperature: Some(0.0),
|
temperature: Some(0.0),
|
||||||
|
top_p: None,
|
||||||
|
reasoning_effort: None,
|
||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.client
|
match self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header("api-key", &self.api_key)
|
.header("api-key", &self.api_key)
|
||||||
.json(&request)
|
.json(&request)
|
||||||
@@ -343,3 +600,60 @@ impl LlmProvider for AzureOpenAiClient {
|
|||||||
"azure-openai"
|
"azure-openai"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Available OpenAI models (including o-series with reasoning)
|
||||||
|
pub const OPENAI_MODELS: &[&str] = &[
|
||||||
|
"o4-mini",
|
||||||
|
"o3",
|
||||||
|
"o3-mini",
|
||||||
|
"o1",
|
||||||
|
"o1-mini",
|
||||||
|
"o1-pro",
|
||||||
|
"gpt-4.1",
|
||||||
|
"gpt-4.1-mini",
|
||||||
|
"gpt-4.1-nano",
|
||||||
|
"gpt-4o",
|
||||||
|
"gpt-4o-mini",
|
||||||
|
"gpt-4-turbo",
|
||||||
|
"gpt-4",
|
||||||
|
"gpt-3.5-turbo",
|
||||||
|
];
|
||||||
|
|
||||||
|
pub fn is_valid_model(model: &str) -> bool {
|
||||||
|
OPENAI_MODELS.contains(&model)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_reasoning_model(model: &str) -> bool {
|
||||||
|
model.starts_with("o")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_model_validation_o_series() {
|
||||||
|
assert!(is_valid_model("o4-mini"));
|
||||||
|
assert!(is_valid_model("o3"));
|
||||||
|
assert!(is_valid_model("o1"));
|
||||||
|
assert!(is_valid_model("gpt-4o"));
|
||||||
|
assert!(is_valid_model("gpt-3.5-turbo"));
|
||||||
|
assert!(!is_valid_model("invalid-model"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_stream_delta_reasoning_parsing() {
|
||||||
|
let json = r#"{"content":null,"reasoning_content":"Let me think..."}"#;
|
||||||
|
let delta: StreamDelta = serde_json::from_str(json).unwrap();
|
||||||
|
assert!(delta.content.is_none());
|
||||||
|
assert_eq!(delta.reasoning_content, Some("Let me think...".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_stream_delta_content_parsing() {
|
||||||
|
let json = r#"{"content":"Hello","reasoning_content":null}"#;
|
||||||
|
let delta: StreamDelta = serde_json::from_str(json).unwrap();
|
||||||
|
assert_eq!(delta.content, Some("Hello".to_string()));
|
||||||
|
assert!(delta.reasoning_content.is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::{create_http_client, LlmProvider};
|
use super::{LlmProvider, create_http_client};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -10,6 +10,9 @@ pub struct OpenRouterClient {
|
|||||||
api_key: String,
|
api_key: String,
|
||||||
model: String,
|
model: String,
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
|
max_tokens: u32,
|
||||||
|
temperature: f32,
|
||||||
|
top_p: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -61,6 +64,9 @@ impl OpenRouterClient {
|
|||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +79,9 @@ impl OpenRouterClient {
|
|||||||
api_key: api_key.to_string(),
|
api_key: api_key.to_string(),
|
||||||
model: model.to_string(),
|
model: model.to_string(),
|
||||||
client,
|
client,
|
||||||
|
max_tokens: 500,
|
||||||
|
temperature: 0.7,
|
||||||
|
top_p: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,27 +91,71 @@ impl OpenRouterClient {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate API key
|
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
|
||||||
pub async fn validate_key(&self) -> Result<bool> {
|
self.max_tokens = max_tokens;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_temperature(mut self, temperature: f32) -> Self {
|
||||||
|
self.temperature = temperature;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_top_p(mut self, top_p: f32) -> Self {
|
||||||
|
self.top_p = Some(top_p);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List available models
|
||||||
|
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||||
let url = format!("{}/models", self.base_url);
|
let url = format!("{}/models", self.base_url);
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.header("HTTP-Referer", "https://quicommit.dev")
|
.header("HTTP-Referer", "https://quicommit.dev")
|
||||||
.header("X-Title", "QuiCommit")
|
.header("X-Title", "QuiCommit")
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("Failed to validate OpenRouter API key")?;
|
.context("Failed to list OpenRouter models")?;
|
||||||
|
|
||||||
if response.status().is_success() {
|
if !response.status().is_success() {
|
||||||
Ok(true)
|
|
||||||
} else if response.status().as_u16() == 401 {
|
|
||||||
Ok(false)
|
|
||||||
} else {
|
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
let text = response.text().await.unwrap_or_default();
|
let text = response.text().await.unwrap_or_default();
|
||||||
bail!("OpenRouter API error: {} - {}", status, text)
|
bail!("OpenRouter API error: {} - {}", status, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ModelsResponse {
|
||||||
|
data: Vec<Model>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Model {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let result: ModelsResponse = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.context("Failed to parse OpenRouter response")?;
|
||||||
|
|
||||||
|
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate API key
|
||||||
|
pub async fn validate_key(&self) -> Result<bool> {
|
||||||
|
match self.list_models().await {
|
||||||
|
Ok(_) => Ok(true),
|
||||||
|
Err(e) => {
|
||||||
|
let err_str = e.to_string();
|
||||||
|
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||||
|
Ok(false)
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,12 +163,10 @@ impl OpenRouterClient {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LlmProvider for OpenRouterClient {
|
impl LlmProvider for OpenRouterClient {
|
||||||
async fn generate(&self, prompt: &str) -> Result<String> {
|
async fn generate(&self, prompt: &str) -> Result<String> {
|
||||||
let messages = vec![
|
let messages = vec![Message {
|
||||||
Message {
|
|
||||||
role: "user".to_string(),
|
role: "user".to_string(),
|
||||||
content: prompt.to_string(),
|
content: prompt.to_string(),
|
||||||
},
|
}];
|
||||||
];
|
|
||||||
|
|
||||||
self.chat_completion(messages).await
|
self.chat_completion(messages).await
|
||||||
}
|
}
|
||||||
@@ -154,12 +205,13 @@ impl OpenRouterClient {
|
|||||||
let request = ChatCompletionRequest {
|
let request = ChatCompletionRequest {
|
||||||
model: self.model.clone(),
|
model: self.model.clone(),
|
||||||
messages,
|
messages,
|
||||||
max_tokens: Some(500),
|
max_tokens: Some(self.max_tokens),
|
||||||
temperature: Some(0.7),
|
temperature: Some(self.temperature),
|
||||||
stream: false,
|
stream: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.client
|
let response = self
|
||||||
|
.client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
@@ -177,7 +229,11 @@ impl OpenRouterClient {
|
|||||||
|
|
||||||
// Try to parse error
|
// Try to parse error
|
||||||
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
|
||||||
bail!("OpenRouter API error: {} ({})", error.error.message, error.error.error_type);
|
bail!(
|
||||||
|
"OpenRouter API error: {} ({})",
|
||||||
|
error.error.message,
|
||||||
|
error.error.error_type
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("OpenRouter API error: {} - {}", status, text);
|
bail!("OpenRouter API error: {} - {}", status, text);
|
||||||
@@ -188,7 +244,8 @@ impl OpenRouterClient {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to parse OpenRouter response")?;
|
.context("Failed to parse OpenRouter response")?;
|
||||||
|
|
||||||
result.choices
|
result
|
||||||
|
.choices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.map(|c| c.message.content.trim().to_string())
|
.map(|c| c.message.content.trim().to_string())
|
||||||
@@ -211,7 +268,7 @@ pub const OPENROUTER_MODELS: &[&str] = &[
|
|||||||
];
|
];
|
||||||
|
|
||||||
/// Check if a model name is valid
|
/// Check if a model name is valid
|
||||||
pub fn is_valid_model(model: &str) -> bool {
|
pub fn is_valid_model(_model: &str) -> bool {
|
||||||
// Since OpenRouter supports many models, we'll allow any model name
|
// Since OpenRouter supports many models, we'll allow any model name
|
||||||
// but provide some popular ones as suggestions
|
// but provide some popular ones as suggestions
|
||||||
true
|
true
|
||||||
|
|||||||
151
src/llm/thinking.rs
Normal file
151
src/llm/thinking.rs
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
|
/// 统一的思考状态管理器,用于管理模型思考状态的显示与隐藏
|
||||||
|
pub struct ThinkingStateManager {
|
||||||
|
is_thinking: AtomicBool,
|
||||||
|
on_start: Option<Box<dyn Fn() + Send + Sync>>,
|
||||||
|
on_end: Option<Box<dyn Fn() + Send + Sync>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ThinkingStateManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
is_thinking: AtomicBool::new(false),
|
||||||
|
on_start: None,
|
||||||
|
on_end: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 设置思考开始回调
|
||||||
|
pub fn on_thinking_start<F: Fn() + Send + Sync + 'static>(mut self, callback: F) -> Self {
|
||||||
|
self.on_start = Some(Box::new(callback));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 设置思考结束回调
|
||||||
|
pub fn on_thinking_end<F: Fn() + Send + Sync + 'static>(mut self, callback: F) -> Self {
|
||||||
|
self.on_end = Some(Box::new(callback));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 开始思考状态
|
||||||
|
pub fn start_thinking(&self) {
|
||||||
|
if !self.is_thinking.load(Ordering::SeqCst) {
|
||||||
|
self.is_thinking.store(true, Ordering::SeqCst);
|
||||||
|
if let Some(ref cb) = self.on_start {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 结束思考状态
|
||||||
|
pub fn end_thinking(&self) {
|
||||||
|
if self.is_thinking.load(Ordering::SeqCst) {
|
||||||
|
self.is_thinking.store(false, Ordering::SeqCst);
|
||||||
|
if let Some(ref cb) = self.on_end {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 当前是否处于思考状态
|
||||||
|
pub fn is_thinking(&self) -> bool {
|
||||||
|
self.is_thinking.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ThinkingStateManager {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 线程安全的思考状态管理器引用
|
||||||
|
pub type SharedThinkingState = Arc<ThinkingStateManager>;
|
||||||
|
|
||||||
|
/// 创建带有默认控制台输出的思考状态管理器
|
||||||
|
/// 在思考开始时打印 "thinking...",在思考结束时清除该标识
|
||||||
|
pub fn create_console_thinking_state() -> SharedThinkingState {
|
||||||
|
Arc::new(
|
||||||
|
ThinkingStateManager::new()
|
||||||
|
.on_thinking_start(|| {
|
||||||
|
eprint!("\rthinking...");
|
||||||
|
})
|
||||||
|
.on_thinking_end(|| {
|
||||||
|
eprint!("\r \r");
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_state_transitions() {
|
||||||
|
let manager = ThinkingStateManager::new();
|
||||||
|
assert!(!manager.is_thinking());
|
||||||
|
|
||||||
|
manager.start_thinking();
|
||||||
|
assert!(manager.is_thinking());
|
||||||
|
|
||||||
|
manager.end_thinking();
|
||||||
|
assert!(!manager.is_thinking());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_idempotent_start() {
|
||||||
|
let manager = ThinkingStateManager::new();
|
||||||
|
manager.start_thinking();
|
||||||
|
manager.start_thinking(); // 重复调用不应触发回调两次
|
||||||
|
assert!(manager.is_thinking());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_idempotent_end() {
|
||||||
|
let manager = ThinkingStateManager::new();
|
||||||
|
manager.end_thinking(); // 未开始时结束不应触发问题
|
||||||
|
assert!(!manager.is_thinking());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_thinking_callbacks() {
|
||||||
|
let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let events_clone = events.clone();
|
||||||
|
|
||||||
|
let manager = ThinkingStateManager::new().on_thinking_start(move || {
|
||||||
|
events_clone.lock().unwrap().push("start".to_string());
|
||||||
|
});
|
||||||
|
|
||||||
|
let events_clone2 = events.clone();
|
||||||
|
let manager = manager.on_thinking_end(move || {
|
||||||
|
events_clone2.lock().unwrap().push("end".to_string());
|
||||||
|
});
|
||||||
|
|
||||||
|
manager.start_thinking();
|
||||||
|
manager.end_thinking();
|
||||||
|
|
||||||
|
let recorded = events.lock().unwrap();
|
||||||
|
assert_eq!(recorded.len(), 2);
|
||||||
|
assert_eq!(recorded[0], "start");
|
||||||
|
assert_eq!(recorded[1], "end");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_create_console_thinking_state() {
|
||||||
|
let state = create_console_thinking_state();
|
||||||
|
assert!(!state.is_thinking());
|
||||||
|
state.start_thinking();
|
||||||
|
assert!(state.is_thinking());
|
||||||
|
state.end_thinking();
|
||||||
|
assert!(!state.is_thinking());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_default() {
|
||||||
|
let manager = ThinkingStateManager::default();
|
||||||
|
assert!(!manager.is_thinking());
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/main.rs
24
src/main.rs
@@ -1,17 +1,21 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use std::path::PathBuf;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod config;
|
mod config;
|
||||||
mod generator;
|
mod generator;
|
||||||
mod git;
|
mod git;
|
||||||
|
mod i18n;
|
||||||
mod llm;
|
mod llm;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use commands::{
|
use commands::{
|
||||||
changelog::ChangelogCommand, commit::CommitCommand, config::ConfigCommand,
|
changelog::ChangelogCommand, commit::CommitCommand, config::ConfigCommand, init::InitCommand,
|
||||||
init::InitCommand, profile::ProfileCommand, tag::TagCommand,
|
profile::ProfileCommand, tag::TagCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// QuiCommit - AI-powered Git assistant
|
/// QuiCommit - AI-powered Git assistant
|
||||||
@@ -73,7 +77,6 @@ enum Commands {
|
|||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
// Initialize logging
|
|
||||||
let log_level = match cli.verbose {
|
let log_level = match cli.verbose {
|
||||||
0 => "warn",
|
0 => "warn",
|
||||||
1 => "info",
|
1 => "info",
|
||||||
@@ -88,13 +91,14 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
debug!("Starting quicommit v{}", env!("CARGO_PKG_VERSION"));
|
debug!("Starting quicommit v{}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
// Execute command
|
let config_path: Option<PathBuf> = cli.config.map(PathBuf::from);
|
||||||
|
|
||||||
match cli.command {
|
match cli.command {
|
||||||
Commands::Init(cmd) => cmd.execute().await,
|
Commands::Init(cmd) => cmd.execute(config_path).await,
|
||||||
Commands::Commit(cmd) => cmd.execute().await,
|
Commands::Commit(cmd) => cmd.execute(config_path).await,
|
||||||
Commands::Tag(cmd) => cmd.execute().await,
|
Commands::Tag(cmd) => cmd.execute(config_path).await,
|
||||||
Commands::Changelog(cmd) => cmd.execute().await,
|
Commands::Changelog(cmd) => cmd.execute(config_path).await,
|
||||||
Commands::Profile(cmd) => cmd.execute().await,
|
Commands::Profile(cmd) => cmd.execute(config_path).await,
|
||||||
Commands::Config(cmd) => cmd.execute().await,
|
Commands::Config(cmd) => cmd.execute(config_path).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use aes_gcm::{
|
use aes_gcm::{
|
||||||
aead::{Aead, KeyInit},
|
|
||||||
Aes256Gcm, Nonce,
|
Aes256Gcm, Nonce,
|
||||||
|
aead::{Aead, KeyInit},
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
|
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -20,8 +20,7 @@ pub fn encrypt(data: &[u8], password: &str) -> Result<String> {
|
|||||||
rand::thread_rng().fill(&mut nonce_bytes);
|
rand::thread_rng().fill(&mut nonce_bytes);
|
||||||
|
|
||||||
let key = derive_key(password, &salt)?;
|
let key = derive_key(password, &salt)?;
|
||||||
let cipher = Aes256Gcm::new_from_slice(&key)
|
let cipher = Aes256Gcm::new_from_slice(&key).context("Failed to create cipher")?;
|
||||||
.context("Failed to create cipher")?;
|
|
||||||
let nonce = Nonce::from_slice(&nonce_bytes);
|
let nonce = Nonce::from_slice(&nonce_bytes);
|
||||||
|
|
||||||
let encrypted = cipher
|
let encrypted = cipher
|
||||||
@@ -39,7 +38,8 @@ pub fn encrypt(data: &[u8], password: &str) -> Result<String> {
|
|||||||
|
|
||||||
/// Decrypt data with password
|
/// Decrypt data with password
|
||||||
pub fn decrypt(encrypted_data: &str, password: &str) -> Result<Vec<u8>> {
|
pub fn decrypt(encrypted_data: &str, password: &str) -> Result<Vec<u8>> {
|
||||||
let data = BASE64.decode(encrypted_data)
|
let data = BASE64
|
||||||
|
.decode(encrypted_data)
|
||||||
.context("Invalid base64 encoding")?;
|
.context("Invalid base64 encoding")?;
|
||||||
|
|
||||||
if data.len() < SALT_LEN + NONCE_LEN {
|
if data.len() < SALT_LEN + NONCE_LEN {
|
||||||
@@ -51,8 +51,7 @@ pub fn decrypt(encrypted_data: &str, password: &str) -> Result<Vec<u8>> {
|
|||||||
let encrypted = &data[SALT_LEN + NONCE_LEN..];
|
let encrypted = &data[SALT_LEN + NONCE_LEN..];
|
||||||
|
|
||||||
let key = derive_key(password, salt)?;
|
let key = derive_key(password, salt)?;
|
||||||
let cipher = Aes256Gcm::new_from_slice(&key)
|
let cipher = Aes256Gcm::new_from_slice(&key).context("Failed to create cipher")?;
|
||||||
.context("Failed to create cipher")?;
|
|
||||||
let nonce = Nonce::from_slice(nonce_bytes);
|
let nonce = Nonce::from_slice(nonce_bytes);
|
||||||
|
|
||||||
let decrypted = cipher
|
let decrypted = cipher
|
||||||
@@ -64,7 +63,7 @@ pub fn decrypt(encrypted_data: &str, password: &str) -> Result<Vec<u8>> {
|
|||||||
|
|
||||||
/// Derive key from password using simple method
|
/// Derive key from password using simple method
|
||||||
fn derive_key(password: &str, salt: &[u8]) -> Result<[u8; KEY_LEN]> {
|
fn derive_key(password: &str, salt: &[u8]) -> Result<[u8; KEY_LEN]> {
|
||||||
use sha2::{Sha256, Digest};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
hasher.update(salt);
|
hasher.update(salt);
|
||||||
|
|||||||
@@ -9,14 +9,11 @@ pub fn edit_content(initial_content: &str) -> Result<String> {
|
|||||||
|
|
||||||
/// Edit file in user's default editor
|
/// Edit file in user's default editor
|
||||||
pub fn edit_file(path: &Path) -> Result<String> {
|
pub fn edit_file(path: &Path) -> Result<String> {
|
||||||
let content = fs::read_to_string(path)
|
let content = fs::read_to_string(path).unwrap_or_default();
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let edited = edit::edit(&content)
|
let edited = edit::edit(&content).context("Failed to open editor")?;
|
||||||
.context("Failed to open editor")?;
|
|
||||||
|
|
||||||
fs::write(path, &edited)
|
fs::write(path, &edited).with_context(|| format!("Failed to write file: {:?}", path))?;
|
||||||
.with_context(|| format!("Failed to write file: {:?}", path))?;
|
|
||||||
|
|
||||||
Ok(edited)
|
Ok(edited)
|
||||||
}
|
}
|
||||||
@@ -29,8 +26,7 @@ pub fn edit_temp(initial_content: &str, extension: &str) -> Result<String> {
|
|||||||
.context("Failed to create temp file")?;
|
.context("Failed to create temp file")?;
|
||||||
|
|
||||||
let path = temp_file.path();
|
let path = temp_file.path();
|
||||||
fs::write(path, initial_content)
|
fs::write(path, initial_content).context("Failed to write temp file")?;
|
||||||
.context("Failed to write temp file")?;
|
|
||||||
|
|
||||||
edit_file(path)
|
edit_file(path)
|
||||||
}
|
}
|
||||||
@@ -41,8 +37,22 @@ pub fn get_editor() -> String {
|
|||||||
.or_else(|_| std::env::var("VISUAL"))
|
.or_else(|_| std::env::var("VISUAL"))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
|
if let Ok(_code) = which::which("code") {
|
||||||
|
return "code --wait".to_string();
|
||||||
|
}
|
||||||
|
if let Ok(_notepad) = which::which("notepad") {
|
||||||
|
return "notepad".to_string();
|
||||||
|
}
|
||||||
"notepad".to_string()
|
"notepad".to_string()
|
||||||
|
} else if cfg!(target_os = "macos") {
|
||||||
|
if which::which("code").is_ok() {
|
||||||
|
return "code --wait".to_string();
|
||||||
|
}
|
||||||
|
"vi".to_string()
|
||||||
} else {
|
} else {
|
||||||
|
if which::which("nano").is_ok() {
|
||||||
|
return "nano".to_string();
|
||||||
|
}
|
||||||
"vi".to_string()
|
"vi".to_string()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -51,7 +61,6 @@ pub fn get_editor() -> String {
|
|||||||
/// Check if editor is available
|
/// Check if editor is available
|
||||||
pub fn check_editor() -> Result<()> {
|
pub fn check_editor() -> Result<()> {
|
||||||
let editor = get_editor();
|
let editor = get_editor();
|
||||||
which::which(&editor)
|
which::which(&editor).with_context(|| format!("Editor '{}' not found in PATH", editor))?;
|
||||||
.with_context(|| format!("Editor '{}' not found in PATH", editor))?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use chrono::{DateTime, Local, Utc};
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
/// Format commit message with conventional commit format
|
/// Format commit message with conventional commit format
|
||||||
@@ -12,7 +11,6 @@ pub fn format_conventional_commit(
|
|||||||
) -> String {
|
) -> String {
|
||||||
let mut message = String::new();
|
let mut message = String::new();
|
||||||
|
|
||||||
// Type and scope
|
|
||||||
message.push_str(commit_type);
|
message.push_str(commit_type);
|
||||||
if let Some(s) = scope {
|
if let Some(s) = scope {
|
||||||
message.push_str(&format!("({})", s));
|
message.push_str(&format!("({})", s));
|
||||||
@@ -22,12 +20,10 @@ pub fn format_conventional_commit(
|
|||||||
}
|
}
|
||||||
message.push_str(&format!(": {}", description));
|
message.push_str(&format!(": {}", description));
|
||||||
|
|
||||||
// Body
|
|
||||||
if let Some(b) = body {
|
if let Some(b) = body {
|
||||||
message.push_str(&format!("\n\n{}", b));
|
message.push_str(&format!("\n\n{}", b));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Footer
|
|
||||||
if let Some(f) = footer {
|
if let Some(f) = footer {
|
||||||
message.push_str(&format!("\n\n{}", f));
|
message.push_str(&format!("\n\n{}", f));
|
||||||
}
|
}
|
||||||
@@ -46,26 +42,22 @@ pub fn format_commitlint_commit(
|
|||||||
) -> String {
|
) -> String {
|
||||||
let mut message = String::new();
|
let mut message = String::new();
|
||||||
|
|
||||||
// Header
|
|
||||||
message.push_str(commit_type);
|
message.push_str(commit_type);
|
||||||
if let Some(s) = scope {
|
if let Some(s) = scope {
|
||||||
message.push_str(&format!("({})", s));
|
message.push_str(&format!("({})", s));
|
||||||
}
|
}
|
||||||
message.push_str(&format!(": {}", subject));
|
message.push_str(&format!(": {}", subject));
|
||||||
|
|
||||||
// References
|
|
||||||
if let Some(refs) = references {
|
if let Some(refs) = references {
|
||||||
for reference in refs {
|
for reference in refs {
|
||||||
message.push_str(&format!(" #{}", reference));
|
message.push_str(&format!(" #{}", reference));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Body
|
|
||||||
if let Some(b) = body {
|
if let Some(b) = body {
|
||||||
message.push_str(&format!("\n\n{}", b));
|
message.push_str(&format!("\n\n{}", b));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Footer
|
|
||||||
if let Some(f) = footer {
|
if let Some(f) = footer {
|
||||||
message.push_str(&format!("\n\n{}", f));
|
message.push_str(&format!("\n\n{}", f));
|
||||||
}
|
}
|
||||||
@@ -73,38 +65,11 @@ pub fn format_commitlint_commit(
|
|||||||
message
|
message
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format date for commit message
|
|
||||||
pub fn format_commit_date(date: &DateTime<Local>) -> String {
|
|
||||||
date.format("%Y-%m-%d %H:%M:%S").to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Format date for changelog
|
|
||||||
pub fn format_changelog_date(date: &DateTime<Utc>) -> String {
|
|
||||||
date.format("%Y-%m-%d").to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Format tag name with version
|
|
||||||
pub fn format_tag_name(version: &str, prefix: Option<&str>) -> String {
|
|
||||||
match prefix {
|
|
||||||
Some(p) => format!("{}{}", p, version),
|
|
||||||
None => version.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrap text at specified width
|
/// Wrap text at specified width
|
||||||
pub fn wrap_text(text: &str, width: usize) -> String {
|
pub fn wrap_text(text: &str, width: usize) -> String {
|
||||||
textwrap::fill(text, width)
|
textwrap::fill(text, width)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncate text with ellipsis
|
|
||||||
pub fn truncate(text: &str, max_len: usize) -> String {
|
|
||||||
if text.len() <= max_len {
|
|
||||||
text.to_string()
|
|
||||||
} else {
|
|
||||||
format!("{}...", &text[..max_len.saturating_sub(3)])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clean commit message (remove comments, extra whitespace)
|
/// Clean commit message (remove comments, extra whitespace)
|
||||||
pub fn clean_message(message: &str) -> String {
|
pub fn clean_message(message: &str) -> String {
|
||||||
let comment_regex = Regex::new(r"^#.*$").unwrap();
|
let comment_regex = Regex::new(r"^#.*$").unwrap();
|
||||||
@@ -118,44 +83,6 @@ pub fn clean_message(message: &str) -> String {
|
|||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format list as markdown bullet points
|
|
||||||
pub fn format_markdown_list(items: &[String]) -> String {
|
|
||||||
items
|
|
||||||
.iter()
|
|
||||||
.map(|item| format!("- {}", item))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Format changelog section
|
|
||||||
pub fn format_changelog_section(
|
|
||||||
version: &str,
|
|
||||||
date: &str,
|
|
||||||
changes: &[(String, Vec<String>)],
|
|
||||||
) -> String {
|
|
||||||
let mut section = format!("## [{}] - {}\n\n", version, date);
|
|
||||||
|
|
||||||
for (category, items) in changes {
|
|
||||||
if !items.is_empty() {
|
|
||||||
section.push_str(&format!("### {}\n\n", category));
|
|
||||||
for item in items {
|
|
||||||
section.push_str(&format!("- {}\n", item));
|
|
||||||
}
|
|
||||||
section.push('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
section
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Format git config key
|
|
||||||
pub fn format_git_config_key(section: &str, subsection: Option<&str>, key: &str) -> String {
|
|
||||||
match subsection {
|
|
||||||
Some(sub) => format!("{}.{}.{}", section, sub, key),
|
|
||||||
None => format!("{}.{}", section, key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -189,10 +116,4 @@ mod tests {
|
|||||||
|
|
||||||
assert!(msg.starts_with("feat!: change API response format"));
|
assert!(msg.starts_with("feat!: change API response format"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_truncate() {
|
|
||||||
assert_eq!(truncate("hello", 10), "hello");
|
|
||||||
assert_eq!(truncate("hello world", 8), "hello...");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
350
src/utils/keyring.rs
Normal file
350
src/utils/keyring.rs
Normal file
@@ -0,0 +1,350 @@
|
|||||||
|
use anyhow::{Context, Result, bail};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
const SERVICE_NAME: &str = "quicommit";
|
||||||
|
const ENV_API_KEY: &str = "QUICOMMIT_API_KEY";
|
||||||
|
|
||||||
|
const PAT_SERVICE_PREFIX: &str = "quicommit/pat";
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum KeyringStatus {
|
||||||
|
Available,
|
||||||
|
Unavailable,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct KeyringManager {
|
||||||
|
status: KeyringStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyringManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let status = Self::check_keyring_availability();
|
||||||
|
Self { status }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_keyring_availability() -> KeyringStatus {
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
KeyringStatus::Available
|
||||||
|
}
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
{
|
||||||
|
KeyringStatus::Available
|
||||||
|
}
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
|
Self::check_linux_keyring()
|
||||||
|
}
|
||||||
|
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
||||||
|
{
|
||||||
|
KeyringStatus::Unavailable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
fn check_linux_keyring() -> KeyringStatus {
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
let has_dbus = Path::new("/usr/bin/dbus-daemon").exists()
|
||||||
|
|| Path::new("/bin/dbus-daemon").exists()
|
||||||
|
|| env::var("DBUS_SESSION_BUS_ADDRESS").is_ok();
|
||||||
|
|
||||||
|
let has_keyring = Path::new("/usr/bin/gnome-keyring-daemon").exists()
|
||||||
|
|| Path::new("/usr/bin/gnome-keyring").exists()
|
||||||
|
|| Path::new("/usr/bin/kwalletd5").exists()
|
||||||
|
|| Path::new("/usr/bin/kwalletd6").exists()
|
||||||
|
|| env::var("SECRET_SERVICE").is_ok();
|
||||||
|
|
||||||
|
if has_dbus && has_keyring {
|
||||||
|
KeyringStatus::Available
|
||||||
|
} else {
|
||||||
|
KeyringStatus::Unavailable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn status(&self) -> KeyringStatus {
|
||||||
|
self.status
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_available(&self) -> bool {
|
||||||
|
self.status == KeyringStatus::Available
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_api_key(&self, provider: &str, api_key: &str) -> Result<()> {
|
||||||
|
if !self.is_available() {
|
||||||
|
bail!("Keyring is not available on this system");
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||||
|
.context("Failed to create keyring entry")?;
|
||||||
|
|
||||||
|
entry
|
||||||
|
.set_password(api_key)
|
||||||
|
.context("Failed to store API key")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_api_key(&self, provider: &str) -> Result<Option<String>> {
|
||||||
|
if let Ok(key) = env::var(ENV_API_KEY)
|
||||||
|
&& !key.is_empty()
|
||||||
|
{
|
||||||
|
return Ok(Some(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.is_available() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||||
|
.context("Failed to create keyring entry")?;
|
||||||
|
|
||||||
|
match entry.get_password() {
|
||||||
|
Ok(key) => Ok(Some(key)),
|
||||||
|
Err(keyring::Error::NoEntry) => Ok(None),
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_api_key(&self, provider: &str) -> Result<()> {
|
||||||
|
if !self.is_available() {
|
||||||
|
bail!("Keyring is not available on this system");
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||||
|
.context("Failed to create keyring entry")?;
|
||||||
|
|
||||||
|
entry
|
||||||
|
.delete_credential()
|
||||||
|
.context("Failed to delete API key")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_api_key(&self, provider: &str) -> bool {
|
||||||
|
self.get_api_key(provider).unwrap_or(None).is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_pat_service_name(profile_name: &str) -> String {
|
||||||
|
format!("{}/{}", PAT_SERVICE_PREFIX, profile_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_pat(
|
||||||
|
&self,
|
||||||
|
profile_name: &str,
|
||||||
|
user_email: &str,
|
||||||
|
service: &str,
|
||||||
|
token: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
if !self.is_available() {
|
||||||
|
bail!("Keyring is not available on this system");
|
||||||
|
}
|
||||||
|
|
||||||
|
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||||
|
let keyring_user = format!("{}:{}", user_email, service);
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||||
|
.context("Failed to create keyring entry for PAT")?;
|
||||||
|
|
||||||
|
entry
|
||||||
|
.set_password(token)
|
||||||
|
.context("Failed to store PAT in keyring")?;
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"[DEBUG] PAT stored in keyring: service={}, user={}",
|
||||||
|
keyring_service, keyring_user
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pat(
|
||||||
|
&self,
|
||||||
|
profile_name: &str,
|
||||||
|
user_email: &str,
|
||||||
|
service: &str,
|
||||||
|
) -> Result<Option<String>> {
|
||||||
|
if !self.is_available() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||||
|
let keyring_user = format!("{}:{}", user_email, service);
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||||
|
.context("Failed to create keyring entry for PAT")?;
|
||||||
|
|
||||||
|
match entry.get_password() {
|
||||||
|
Ok(token) => {
|
||||||
|
eprintln!(
|
||||||
|
"[DEBUG] PAT retrieved from keyring: service={}, user={}",
|
||||||
|
keyring_service, keyring_user
|
||||||
|
);
|
||||||
|
Ok(Some(token))
|
||||||
|
}
|
||||||
|
Err(keyring::Error::NoEntry) => {
|
||||||
|
eprintln!(
|
||||||
|
"[DEBUG] PAT not found in keyring: service={}, user={}",
|
||||||
|
keyring_service, keyring_user
|
||||||
|
);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_pat(&self, profile_name: &str, user_email: &str, service: &str) -> Result<()> {
|
||||||
|
if !self.is_available() {
|
||||||
|
bail!("Keyring is not available on this system");
|
||||||
|
}
|
||||||
|
|
||||||
|
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||||
|
let keyring_user = format!("{}:{}", user_email, service);
|
||||||
|
|
||||||
|
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||||
|
.context("Failed to create keyring entry for PAT")?;
|
||||||
|
|
||||||
|
entry
|
||||||
|
.delete_credential()
|
||||||
|
.context("Failed to delete PAT from keyring")?;
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"[DEBUG] PAT deleted from keyring: service={}, user={}",
|
||||||
|
keyring_service, keyring_user
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_pat(&self, profile_name: &str, user_email: &str, service: &str) -> bool {
|
||||||
|
self.get_pat(profile_name, user_email, service)
|
||||||
|
.unwrap_or(None)
|
||||||
|
.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_all_pats_for_profile(
|
||||||
|
&self,
|
||||||
|
profile_name: &str,
|
||||||
|
user_email: &str,
|
||||||
|
services: &[String],
|
||||||
|
) -> Result<()> {
|
||||||
|
for service in services {
|
||||||
|
if let Err(e) = self.delete_pat(profile_name, user_email, service) {
|
||||||
|
eprintln!(
|
||||||
|
"[DEBUG] Failed to delete PAT for service '{}': {}",
|
||||||
|
service, e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_status_message(&self) -> String {
|
||||||
|
match self.status {
|
||||||
|
KeyringStatus::Available => {
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
"Windows Credential Manager is available".to_string()
|
||||||
|
}
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
{
|
||||||
|
"macOS Keychain is available".to_string()
|
||||||
|
}
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
|
"Linux secret service is available".to_string()
|
||||||
|
}
|
||||||
|
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
||||||
|
{
|
||||||
|
"Keyring is available".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyringStatus::Unavailable => {
|
||||||
|
"Keyring is not available. Set QUICOMMIT_API_KEY environment variable.".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for KeyringManager {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_default_base_url(provider: &str) -> &'static str {
|
||||||
|
match provider {
|
||||||
|
"openai" => "https://api.openai.com/v1",
|
||||||
|
"anthropic" => "https://api.anthropic.com/v1",
|
||||||
|
"kimi" => "https://api.moonshot.cn/v1",
|
||||||
|
"deepseek" => "https://api.deepseek.com/v1",
|
||||||
|
"openrouter" => "https://openrouter.ai/api/v1",
|
||||||
|
"ollama" => "http://localhost:11434",
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_default_model(provider: &str) -> &'static str {
|
||||||
|
match provider {
|
||||||
|
"openai" => "gpt-4",
|
||||||
|
"anthropic" => "claude-3-sonnet-20240229",
|
||||||
|
"kimi" => "kimi-k2.6",
|
||||||
|
"deepseek" => "deepseek-v4-flash",
|
||||||
|
"openrouter" => "openai/gpt-3.5-turbo",
|
||||||
|
"ollama" => "llama2",
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_supported_providers() -> &'static [&'static str] {
|
||||||
|
&[
|
||||||
|
"ollama",
|
||||||
|
"openai",
|
||||||
|
"anthropic",
|
||||||
|
"kimi",
|
||||||
|
"deepseek",
|
||||||
|
"openrouter",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn provider_needs_api_key(provider: &str) -> bool {
|
||||||
|
provider != "ollama"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_default_base_url() {
|
||||||
|
assert_eq!(get_default_base_url("openai"), "https://api.openai.com/v1");
|
||||||
|
assert_eq!(
|
||||||
|
get_default_base_url("anthropic"),
|
||||||
|
"https://api.anthropic.com/v1"
|
||||||
|
);
|
||||||
|
assert_eq!(get_default_base_url("kimi"), "https://api.moonshot.cn/v1");
|
||||||
|
assert_eq!(
|
||||||
|
get_default_base_url("deepseek"),
|
||||||
|
"https://api.deepseek.com/v1"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_default_base_url("openrouter"),
|
||||||
|
"https://openrouter.ai/api/v1"
|
||||||
|
);
|
||||||
|
assert_eq!(get_default_base_url("ollama"), "http://localhost:11434");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_default_model() {
|
||||||
|
assert_eq!(get_default_model("openai"), "gpt-4");
|
||||||
|
assert_eq!(get_default_model("anthropic"), "claude-3-sonnet-20240229");
|
||||||
|
assert_eq!(get_default_model("ollama"), "llama2");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_provider_needs_api_key() {
|
||||||
|
assert!(provider_needs_api_key("openai"));
|
||||||
|
assert!(provider_needs_api_key("anthropic"));
|
||||||
|
assert!(!provider_needs_api_key("ollama"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod editor;
|
pub mod editor;
|
||||||
pub mod formatter;
|
pub mod formatter;
|
||||||
|
pub mod keyring;
|
||||||
pub mod validators;
|
pub mod validators;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{Result, bail};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
@@ -58,11 +58,6 @@ lazy_static! {
|
|||||||
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
|
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
/// Regex for SSH key validation (basic)
|
|
||||||
static ref SSH_KEY_REGEX: Regex = Regex::new(
|
|
||||||
r"^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521)\s+[A-Za-z0-9+/]+={0,2}\s+.*$"
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
/// Regex for GPG key ID validation
|
/// Regex for GPG key ID validation
|
||||||
static ref GPG_KEY_ID_REGEX: Regex = Regex::new(
|
static ref GPG_KEY_ID_REGEX: Regex = Regex::new(
|
||||||
r"^[A-F0-9]{16,40}$"
|
r"^[A-F0-9]{16,40}$"
|
||||||
@@ -81,7 +76,6 @@ pub fn validate_conventional_commit(message: &str) -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check description length (max 100 chars for first line)
|
|
||||||
if first_line.len() > 100 {
|
if first_line.len() > 100 {
|
||||||
bail!("Commit subject too long (max 100 characters)");
|
bail!("Commit subject too long (max 100 characters)");
|
||||||
}
|
}
|
||||||
@@ -93,7 +87,6 @@ pub fn validate_conventional_commit(message: &str) -> Result<()> {
|
|||||||
pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
||||||
let first_line = message.lines().next().unwrap_or("");
|
let first_line = message.lines().next().unwrap_or("");
|
||||||
|
|
||||||
// Commitlint is more lenient but still requires type prefix
|
|
||||||
let parts: Vec<&str> = first_line.splitn(2, ':').collect();
|
let parts: Vec<&str> = first_line.splitn(2, ':').collect();
|
||||||
if parts.len() != 2 {
|
if parts.len() != 2 {
|
||||||
bail!("Invalid commit format. Expected: <type>[optional scope]: <subject>");
|
bail!("Invalid commit format. Expected: <type>[optional scope]: <subject>");
|
||||||
@@ -102,7 +95,6 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
|||||||
let type_part = parts[0];
|
let type_part = parts[0];
|
||||||
let subject = parts[1].trim();
|
let subject = parts[1].trim();
|
||||||
|
|
||||||
// Extract type (handle scope and breaking indicator)
|
|
||||||
let commit_type = type_part
|
let commit_type = type_part
|
||||||
.split('(')
|
.split('(')
|
||||||
.next()
|
.next()
|
||||||
@@ -117,7 +109,6 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate subject
|
|
||||||
if subject.is_empty() {
|
if subject.is_empty() {
|
||||||
bail!("Commit subject cannot be empty");
|
bail!("Commit subject cannot be empty");
|
||||||
}
|
}
|
||||||
@@ -130,12 +121,15 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
|||||||
bail!("Commit subject too long (max 100 characters)");
|
bail!("Commit subject too long (max 100 characters)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subject should not start with uppercase
|
if subject
|
||||||
if subject.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
|
.chars()
|
||||||
|
.next()
|
||||||
|
.map(|c| c.is_uppercase())
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
bail!("Commit subject should not start with uppercase letter");
|
bail!("Commit subject should not start with uppercase letter");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subject should not end with period
|
|
||||||
if subject.ends_with('.') {
|
if subject.ends_with('.') {
|
||||||
bail!("Commit subject should not end with a period");
|
bail!("Commit subject should not end with a period");
|
||||||
}
|
}
|
||||||
@@ -179,15 +173,6 @@ pub fn validate_email(email: &str) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate SSH key format
|
|
||||||
pub fn validate_ssh_key(key: &str) -> Result<()> {
|
|
||||||
if !SSH_KEY_REGEX.is_match(key.trim()) {
|
|
||||||
bail!("Invalid SSH public key format");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Validate GPG key ID
|
/// Validate GPG key ID
|
||||||
pub fn validate_gpg_key_id(key_id: &str) -> Result<()> {
|
pub fn validate_gpg_key_id(key_id: &str) -> Result<()> {
|
||||||
if !GPG_KEY_ID_REGEX.is_match(key_id) {
|
if !GPG_KEY_ID_REGEX.is_match(key_id) {
|
||||||
@@ -207,7 +192,10 @@ pub fn validate_profile_name(name: &str) -> Result<()> {
|
|||||||
bail!("Profile name too long (max 50 characters)");
|
bail!("Profile name too long (max 50 characters)");
|
||||||
}
|
}
|
||||||
|
|
||||||
if !name.chars().all(|c| c.is_alphanumeric() || c == '-' || c == '_') {
|
if !name
|
||||||
|
.chars()
|
||||||
|
.all(|c| c.is_alphanumeric() || c == '-' || c == '_')
|
||||||
|
{
|
||||||
bail!("Profile name can only contain letters, numbers, hyphens, and underscores");
|
bail!("Profile name can only contain letters, numbers, hyphens, and underscores");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
434
tests/config_export_import_tests.rs
Normal file
434
tests/config_export_import_tests.rs
Normal file
@@ -0,0 +1,434 @@
|
|||||||
|
use assert_cmd::cargo::cargo_bin_cmd;
|
||||||
|
use predicates::prelude::*;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
fn init_quicommit(config_path: &PathBuf) {
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
|
|
||||||
|
mod config_export {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_export_to_stdout() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
init_quicommit(&config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("version"))
|
||||||
|
.stdout(predicate::str::contains("[llm]"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_export_to_file() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
let export_path = temp_dir.path().join("exported.toml");
|
||||||
|
init_quicommit(&config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"",
|
||||||
|
]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Configuration exported"));
|
||||||
|
|
||||||
|
assert!(export_path.exists(), "Export file should be created");
|
||||||
|
|
||||||
|
let content = fs::read_to_string(&export_path).unwrap();
|
||||||
|
assert!(content.contains("version"), "Export should contain version");
|
||||||
|
assert!(
|
||||||
|
content.contains("[llm]"),
|
||||||
|
"Export should contain LLM config"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_export_encrypted() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
let export_path = temp_dir.path().join("encrypted.toml");
|
||||||
|
init_quicommit(&config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"test_password_123",
|
||||||
|
]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("encrypted and exported"));
|
||||||
|
|
||||||
|
assert!(export_path.exists(), "Export file should be created");
|
||||||
|
|
||||||
|
let content = fs::read_to_string(&export_path).unwrap();
|
||||||
|
assert!(
|
||||||
|
content.starts_with("ENCRYPTED:"),
|
||||||
|
"Encrypted file should start with ENCRYPTED:"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!content.contains("[llm]"),
|
||||||
|
"Encrypted content should not be readable"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod config_import {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_import_plain_config() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
let import_path = temp_dir.path().join("import.toml");
|
||||||
|
|
||||||
|
let plain_config = r#"
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[llm]
|
||||||
|
provider = "openai"
|
||||||
|
model = "gpt-4"
|
||||||
|
max_tokens = 1000
|
||||||
|
temperature = 0.7
|
||||||
|
timeout = 60
|
||||||
|
api_key_storage = "keyring"
|
||||||
|
|
||||||
|
[commit]
|
||||||
|
format = "conventional"
|
||||||
|
auto_generate = true
|
||||||
|
allow_empty = false
|
||||||
|
gpg_sign = false
|
||||||
|
max_subject_length = 100
|
||||||
|
require_scope = false
|
||||||
|
require_body = false
|
||||||
|
body_required_types = ["feat", "fix"]
|
||||||
|
|
||||||
|
[tag]
|
||||||
|
version_prefix = "v"
|
||||||
|
auto_generate = true
|
||||||
|
gpg_sign = false
|
||||||
|
include_changelog = true
|
||||||
|
|
||||||
|
[changelog]
|
||||||
|
path = "CHANGELOG.md"
|
||||||
|
auto_generate = true
|
||||||
|
format = "keep-a-changelog"
|
||||||
|
include_hashes = false
|
||||||
|
include_authors = false
|
||||||
|
group_by_type = true
|
||||||
|
|
||||||
|
[theme]
|
||||||
|
colors = true
|
||||||
|
icons = true
|
||||||
|
date_format = "%Y-%m-%d"
|
||||||
|
|
||||||
|
[language]
|
||||||
|
output_language = "en"
|
||||||
|
keep_types_english = true
|
||||||
|
keep_changelog_types_english = true
|
||||||
|
"#;
|
||||||
|
fs::write(&import_path, plain_config).unwrap();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"import",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
"--file",
|
||||||
|
import_path.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Configuration imported"));
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"get",
|
||||||
|
"llm.provider",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("openai"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_import_encrypted_config() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path1 = temp_dir.path().join("config1.toml");
|
||||||
|
let config_path2 = temp_dir.path().join("config2.toml");
|
||||||
|
let export_path = temp_dir.path().join("encrypted.toml");
|
||||||
|
|
||||||
|
init_quicommit(&config_path1);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"set",
|
||||||
|
"llm.provider",
|
||||||
|
"anthropic",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"secure_password",
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"import",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
"--file",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"secure_password",
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Configuration imported"));
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"get",
|
||||||
|
"llm.provider",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("anthropic"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_import_encrypted_wrong_password() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
let export_path = temp_dir.path().join("encrypted.toml");
|
||||||
|
|
||||||
|
init_quicommit(&config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"correct_password",
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"import",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
"--file",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"wrong_password",
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("Failed to decrypt"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod config_export_import_roundtrip {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_roundtrip_plain() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path1 = temp_dir.path().join("config1.toml");
|
||||||
|
let config_path2 = temp_dir.path().join("config2.toml");
|
||||||
|
let export_path = temp_dir.path().join("export.toml");
|
||||||
|
|
||||||
|
init_quicommit(&config_path1);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"set",
|
||||||
|
"llm.model",
|
||||||
|
"gpt-4-turbo",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
"",
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"import",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
"--file",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"get",
|
||||||
|
"llm.model",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("gpt-4-turbo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_roundtrip_encrypted() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path1 = temp_dir.path().join("config1.toml");
|
||||||
|
let config_path2 = temp_dir.path().join("config2.toml");
|
||||||
|
let export_path = temp_dir.path().join("encrypted.toml");
|
||||||
|
let password = "my_secure_password_123";
|
||||||
|
|
||||||
|
init_quicommit(&config_path1);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"set",
|
||||||
|
"llm.provider",
|
||||||
|
"deepseek",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"set",
|
||||||
|
"llm.model",
|
||||||
|
"deepseek-chat",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"export",
|
||||||
|
"--config",
|
||||||
|
config_path1.to_str().unwrap(),
|
||||||
|
"--output",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
password,
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let exported_content = fs::read_to_string(&export_path).unwrap();
|
||||||
|
assert!(exported_content.starts_with("ENCRYPTED:"));
|
||||||
|
assert!(!exported_content.contains("deepseek"));
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"import",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
"--file",
|
||||||
|
export_path.to_str().unwrap(),
|
||||||
|
"--password",
|
||||||
|
password,
|
||||||
|
]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"get",
|
||||||
|
"llm.provider",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("deepseek"));
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"get",
|
||||||
|
"llm.model",
|
||||||
|
"--config",
|
||||||
|
config_path2.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("deepseek-chat"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,64 +1,831 @@
|
|||||||
use assert_cmd::Command;
|
use assert_cmd::cargo::cargo_bin_cmd;
|
||||||
use predicates::prelude::*;
|
use predicates::prelude::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
#[test]
|
fn create_git_repo(dir: &PathBuf) -> std::process::Output {
|
||||||
fn test_cli_help() {
|
std::process::Command::new("git")
|
||||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
.args(&["init"])
|
||||||
cmd.arg("--help");
|
.current_dir(dir)
|
||||||
cmd.assert()
|
.output()
|
||||||
.success()
|
.expect("Failed to init git repo")
|
||||||
.stdout(predicate::str::contains("QuiCommit"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn configure_git_user(dir: &PathBuf) {
|
||||||
fn test_version() {
|
std::process::Command::new("git")
|
||||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
.args(&["config", "user.name", "Test User"])
|
||||||
cmd.arg("--version");
|
.current_dir(dir)
|
||||||
cmd.assert()
|
.output()
|
||||||
.success()
|
.expect("Failed to configure git user name");
|
||||||
.stdout(predicate::str::contains("0.1.0"));
|
|
||||||
|
std::process::Command::new("git")
|
||||||
|
.args(&["config", "user.email", "test@example.com"])
|
||||||
|
.current_dir(dir)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to configure git user email");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn create_test_file(dir: &PathBuf, name: &str, content: &str) {
|
||||||
fn test_config_show() {
|
let file_path = dir.join(name);
|
||||||
let temp_dir = TempDir::new().unwrap();
|
fs::write(&file_path, content).expect("Failed to create test file");
|
||||||
let config_dir = temp_dir.path().join("config");
|
}
|
||||||
fs::create_dir(&config_dir).unwrap();
|
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
fn stage_file(dir: &PathBuf, name: &str) {
|
||||||
cmd.env("QUICOMMIT_CONFIG", config_dir.join("config.toml"))
|
std::process::Command::new("git")
|
||||||
.arg("config")
|
.args(&["add", name])
|
||||||
.arg("show");
|
.current_dir(dir)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to stage file");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_commit(dir: &PathBuf, message: &str) {
|
||||||
|
std::process::Command::new("git")
|
||||||
|
.args(&["commit", "-m", message])
|
||||||
|
.current_dir(dir)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to create commit");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_git_repo(dir: &PathBuf) {
|
||||||
|
create_git_repo(dir);
|
||||||
|
configure_git_user(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_test_repo_with_file(dir: &PathBuf, file_name: &str, file_content: &str) {
|
||||||
|
setup_git_repo(dir);
|
||||||
|
create_test_file(dir, file_name, file_content);
|
||||||
|
stage_file(dir, file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_quicommit(dir: &PathBuf, config_path: &PathBuf) {
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||||
|
.current_dir(dir);
|
||||||
cmd.assert().success();
|
cmd.assert().success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
mod cli_basic {
|
||||||
fn test_profile_list_empty() {
|
use super::*;
|
||||||
let temp_dir = TempDir::new().unwrap();
|
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
|
||||||
cmd.env("QUICOMMIT_CONFIG", temp_dir.path().join("config.toml"))
|
|
||||||
.arg("profile")
|
|
||||||
.arg("list");
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_help() {
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.arg("--help");
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::str::contains("No profiles configured"));
|
.stdout(predicate::str::contains("QuiCommit"))
|
||||||
|
.stdout(predicate::str::contains("AI-powered Git assistant"))
|
||||||
|
.stdout(predicate::str::contains("Usage:"))
|
||||||
|
.stdout(predicate::str::contains("Commands:"))
|
||||||
|
.stdout(predicate::str::contains("Options:"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_version() {
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.arg("--version");
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("quicommit"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_no_args_shows_help() {
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("Usage:"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verbose_flag() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
create_git_repo(&repo_path);
|
||||||
|
configure_git_user(&repo_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"-vv",
|
||||||
|
"init",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
mod init_command {
|
||||||
fn test_init_quick() {
|
use super::*;
|
||||||
let temp_dir = TempDir::new().unwrap();
|
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
#[test]
|
||||||
cmd.env("QUICOMMIT_CONFIG", temp_dir.path().join("config.toml"))
|
fn test_init_quick() {
|
||||||
.arg("init")
|
let temp_dir = TempDir::new().unwrap();
|
||||||
.arg("--yes");
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::str::contains("initialized successfully"));
|
.stdout(predicate::str::contains("initialized successfully"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_creates_config_file() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
assert!(config_path.exists(), "Config file should be created");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_in_git_repo() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
create_git_repo(&repo_path);
|
||||||
|
configure_git_user(&repo_path);
|
||||||
|
|
||||||
|
let config_path = repo_path.join("test_config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_reset() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"init",
|
||||||
|
"--yes",
|
||||||
|
"--reset",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("initialized successfully"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod profile_command {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_profile_list_empty() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["profile", "list", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("No profiles"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_profile_list_with_profile() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["profile", "list", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("default"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod config_command {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_show() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["config", "show", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Configuration"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_path() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["config", "path", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("config.toml"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod commit_command {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_no_repo() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(temp_dir.path());
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_no_changes() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_git_repo(&repo_path);
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"test: empty",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Dry run"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_with_staged_changes() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "Hello, World!");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"test: add test file",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Dry run"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_date_mode() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "daily.txt", "Daily update");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--date",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Dry run"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_with_think_flag() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "Hello, World!");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--think",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"test: think flag",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod tag_command {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tag_no_repo() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"tag",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(temp_dir.path());
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tag_list_empty() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_git_repo(&repo_path);
|
||||||
|
|
||||||
|
create_test_file(&repo_path, "test.txt", "content");
|
||||||
|
stage_file(&repo_path, "test.txt");
|
||||||
|
create_commit(&repo_path, "feat: initial commit");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"tag",
|
||||||
|
"--name",
|
||||||
|
"v0.1.0",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("v0.1.0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tag_with_think_flag() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_git_repo(&repo_path);
|
||||||
|
|
||||||
|
create_test_file(&repo_path, "test.txt", "content");
|
||||||
|
stage_file(&repo_path, "test.txt");
|
||||||
|
create_commit(&repo_path, "feat: initial commit");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"tag",
|
||||||
|
"--think",
|
||||||
|
"--name",
|
||||||
|
"v0.2.0",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod changelog_command {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_changelog_init() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_git_repo(&repo_path);
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
let changelog_path = repo_path.join("CHANGELOG.md");
|
||||||
|
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"changelog",
|
||||||
|
"--init",
|
||||||
|
"--output",
|
||||||
|
changelog_path.to_str().unwrap(),
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
assert!(changelog_path.exists(), "Changelog file should be created");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_changelog_dry_run() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_git_repo(&repo_path);
|
||||||
|
|
||||||
|
create_test_file(&repo_path, "test.txt", "content");
|
||||||
|
stage_file(&repo_path, "test.txt");
|
||||||
|
create_commit(&repo_path, "feat: add feature");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"changelog",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod cross_platform {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_path_handling_windows_style() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("subdir").join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
assert!(config_path.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_with_spaces_in_path() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let space_dir = temp_dir.path().join("path with spaces");
|
||||||
|
fs::create_dir_all(&space_dir).unwrap();
|
||||||
|
let config_path = space_dir.join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
assert!(config_path.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_with_unicode_path() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let unicode_dir = temp_dir.path().join("路径测试");
|
||||||
|
fs::create_dir_all(&unicode_dir).unwrap();
|
||||||
|
let config_path = unicode_dir.join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert().success();
|
||||||
|
assert!(config_path.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod git_operations {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_git_repo_detection() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
create_git_repo(&repo_path);
|
||||||
|
configure_git_user(&repo_path);
|
||||||
|
|
||||||
|
let git_dir = repo_path.join(".git");
|
||||||
|
assert!(git_dir.exists(), ".git directory should exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_git_status_clean() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
create_git_repo(&repo_path);
|
||||||
|
configure_git_user(&repo_path);
|
||||||
|
|
||||||
|
let output = std::process::Command::new("git")
|
||||||
|
.args(&["status", "--porcelain"])
|
||||||
|
.current_dir(&repo_path)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to run git status");
|
||||||
|
|
||||||
|
assert!(output.status.success());
|
||||||
|
assert!(String::from_utf8_lossy(&output.stdout).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_git_commit_creation() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
create_git_repo(&repo_path);
|
||||||
|
configure_git_user(&repo_path);
|
||||||
|
|
||||||
|
create_test_file(&repo_path, "test.txt", "content");
|
||||||
|
stage_file(&repo_path, "test.txt");
|
||||||
|
create_commit(&repo_path, "feat: initial commit");
|
||||||
|
|
||||||
|
let output = std::process::Command::new("git")
|
||||||
|
.args(&["log", "--oneline"])
|
||||||
|
.current_dir(&repo_path)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to run git log");
|
||||||
|
|
||||||
|
assert!(output.status.success());
|
||||||
|
let log = String::from_utf8_lossy(&output.stdout);
|
||||||
|
assert!(log.contains("initial commit"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod validators {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_message_validation() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"invalid commit message without type",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("Invalid").or(predicate::str::contains("format")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_valid_conventional_commit() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"feat: add new feature",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Dry run"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod subcommands {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_commit_alias() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"c",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"fix: test",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Dry run"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_alias() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["i", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("initialized successfully"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_profile_alias() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let config_path = temp_dir.path().join("config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["p", "list", "--config", config_path.to_str().unwrap()]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("default"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod edge_cases {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_file_not_found() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let non_existent_config = temp_dir.path().join("non_existent_config.toml");
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"config",
|
||||||
|
"show",
|
||||||
|
"--config",
|
||||||
|
non_existent_config.to_str().unwrap(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("QuiCommit Configuration"))
|
||||||
|
.stdout(predicate::str::contains("Default profile: (none)"))
|
||||||
|
.stdout(predicate::str::contains("Profiles: 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_git_repo() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||||
|
cmd.assert().success();
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty_commit_message() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let repo_path = temp_dir.path().to_path_buf();
|
||||||
|
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||||
|
|
||||||
|
let config_path = repo_path.join("config.toml");
|
||||||
|
init_quicommit(&repo_path, &config_path);
|
||||||
|
|
||||||
|
let mut cmd = cargo_bin_cmd!("quicommit");
|
||||||
|
cmd.args(&[
|
||||||
|
"commit",
|
||||||
|
"--manual",
|
||||||
|
"-m",
|
||||||
|
"",
|
||||||
|
"--dry-run",
|
||||||
|
"--yes",
|
||||||
|
"--config",
|
||||||
|
config_path.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.current_dir(&repo_path);
|
||||||
|
|
||||||
|
cmd.assert().failure().stderr(predicate::str::contains(
|
||||||
|
"Invalid conventional commit format",
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user