程式碼壓縮
程式碼壓縮是一個強大的功能,它能夠在移除實現細節的同時智能提取關鍵程式碼結構。在需要減少令牌數量的同時保持程式碼庫的重要結構信息時,這個功能特別有用。
NOTE
這是一個實驗性功能,我們將根據用戶反饋和實際使用情況積極改進。
基本用法
使用 --compress
標誌啟用程式碼壓縮:
bash
repomix --compress
也可以在遠端倉庫中使用:
bash
repomix --remote user/repo --compress
工作原理
壓縮算法使用 Tree-sitter 解析處理程式碼,提取並保留基本結構元素,同時移除實現細節。
壓縮會保留:
- 函數和方法簽名
- 介面和類型定義
- 類結構和屬性
- 重要的結構元素
同時會移除:
- 函數和方法實現
- 迴圈和條件邏輯細節
- 內部變量聲明
- 具體實現程式碼
示例
原始 TypeScript 程式碼:
typescript
import { ShoppingItem } from './shopping-item';
/**
* Calculate the total price of shopping items
*/
const calculateTotal = (
items: ShoppingItem[]
) => {
let total = 0;
for (const item of items) {
total += item.price * item.quantity;
}
return total;
}
// Shopping item interface
interface Item {
name: string;
price: number;
quantity: number;
}
壓縮後:
typescript
import { ShoppingItem } from './shopping-item';
⋮----
/**
* Calculate the total price of shopping items
*/
const calculateTotal = (
items: ShoppingItem[]
) => {
⋮----
// Shopping item interface
interface Item {
name: string;
price: number;
quantity: number;
}
配置
你可以在配置文件中啟用壓縮:
json
{
"output": {
"compress": true
}
}
使用場景
程式碼壓縮在以下情況特別有用:
- 分析程式碼結構和架構
- 減少用於 LLM 處理的令牌數量