Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Lorebyte

[English](README.md) | [中文](README_zh.md)

AI-powered git commit message generator for VS Code.

Lorebyte reads your staged changes, sends the diff to an LLM, and writes a [Conventional Commits](https://www.conventionalcommits.org/) message directly into the Source Control input box — one click, done.

## Features

- **One-click commit messages** — click the Lorebyte icon in the Source Control title bar or run the command from the palette
- **Smart generation** — if the commit input box is empty, generates a message from scratch; if it already contains text, refines and optimizes the existing message based on the staged diff
- **Conventional Commits** — generates messages following the `<type>(<scope>): <description>` format
- **Chinese / English** — commit messages can be generated in Chinese or English
- **Model selection** — browse and switch between available models via API
Expand Down
94 changes: 94 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Lorebyte

[English](README.md) | [中文](README_zh.md)

基于 AI 的 VS Code Git 提交信息生成器。

Lorebyte 读取你的暂存更改,将 diff 发送给 LLM,然后将符合 [Conventional Commits](https://www.conventionalcommits.org/) 规范的提交信息直接写入源代码管理输入框 — 一键搞定。

## 功能特性

- **一键生成** — 点击源代码管理标题栏中的 Lorebyte 图标,或通过命令面板运行
- **智能生成** — 输入框为空时从头生成提交信息;已有内容时基于暂存的 diff 优化和完善现有信息
- **Conventional Commits** — 生成符合 `<type>(<scope>): <description>` 格式的提交信息
- **中英文支持** — 提交信息可以生成中文或英文
- **模型选择** — 通过 API 浏览并切换可用模型
- **可配置的 Provider** — 内置 [OpenCode Zen](https://opencode.ai/docs/zen/) 支持,可通过 Provider 注册表扩展

## 快速开始

1. 安装扩展
2. 在设置中配置 API Key:**Lorebyte > Api Key**
3. 在 git 中暂存一些更改
4. 点击源代码管理标题栏中的 Lorebyte 图标(或在命令面板中运行 `Lorebyte: Generate Commit Message`)

生成的提交信息会出现在提交输入框中,可以直接提交。

## 命令

| 命令 | 说明 |
|------|------|
| `Lorebyte: Generate Commit Message` | 根据暂存更改生成提交信息 |
| `Lorebyte: List Available Models` | 浏览远程模型并切换当前使用的模型(需要 API Key) |

## 设置

| 设置项 | 默认值 | 说明 |
|--------|--------|------|
| `lorebyte.provider` | `opencode-zen` | LLM Provider ID |
| `lorebyte.model` | `minimax-m2.5-free` | 用于生成提交信息的模型 ID |
| `lorebyte.apiKey` | — | LLM Provider 的 API Key |
| `lorebyte.apiBaseUrl` | `https://opencode.ai/zen/v1/chat/completions` | API 端点 URL |
| `lorebyte.language` | `English` | 生成提交信息的语言(`English` / `Chinese`) |

## 开发

### 环境要求

- Node.js >= 18
- VS Code >= 1.85.0

### 构建

```bash
npm install
npm run compile
```

### 监听模式

```bash
npm run watch
```

### 代码检查

```bash
npm run lint
```

在 VS Code 中按 **F5** 启动扩展开发宿主,加载该扩展进行调试。

## 项目结构

```
src/
├── extension.ts # 入口文件 — 注册命令和 Provider
├── commands/
│ ├── generateCommitMessage.ts # 核心流程:diff → LLM → 提交信息
│ └── listModels.ts # 浏览和选择模型
├── config/
│ └── configuration.ts # 读取 VS Code 设置
├── git/
│ └── gitService.ts # 通过 VS Code Git API 获取暂存 diff 和提交信息
├── prompt/
│ └── commitPrompt.ts # 系统提示词和用户提示词构建
└── providers/
├── types.ts # LLMProvider 接口和消息类型
├── providerRegistry.ts # Provider 注册表模式
└── openCodeZenProvider.ts # OpenCode Zen 实现
```

## 许可证

MIT
9 changes: 8 additions & 1 deletion src/commands/generateCommitMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ export async function generateCommitMessage(): Promise<void> {
},
async () => {
try {
const currentMessage = gitService.getCommitMessage().trim();
const response = await provider.chatCompletion(
{
model: config.model,
messages: [
{ role: "system", content: buildSystemPrompt(config.language) },
{ role: "user", content: buildUserPrompt(diff) },
{
role: "user",
content: buildUserPrompt(
diff,
currentMessage || undefined
),
},
],
},
config.apiKey,
Expand Down
5 changes: 5 additions & 0 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export class GitService {
return diff;
}

getCommitMessage(): string {
const repo = this.getRepository();
return repo.inputBox.value;
}

setCommitMessage(message: string): void {
const repo = this.getRepository();
repo.inputBox.value = message;
Expand Down
8 changes: 7 additions & 1 deletion src/prompt/commitPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ Available types:
Respond with ONLY the commit message, nothing else. No quotes, no markdown, no explanation.`;
}

export function buildUserPrompt(diff: string): string {
export function buildUserPrompt(
diff: string,
currentMessage?: string
): string {
const truncated = truncateDiff(diff);
if (currentMessage) {
return `The current commit message is:\n\n${currentMessage}\n\nOptimize and refine this commit message based on the staged changes below. Keep the original intent but improve clarity, accuracy, and adherence to Conventional Commits format.\n\n\`\`\`diff\n${truncated}\n\`\`\``;
}
return `Generate a commit message for the following staged changes:\n\n\`\`\`diff\n${truncated}\n\`\`\``;
}