ai_xiaopei
9 days ago cf162a137caffa5f25f646220a636d78e8253864
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cmd
 
import (
    "fmt"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
    "time"
 
    "github.com/aisim/kb-cli/internal/index"
    "github.com/spf13/cobra"
)
 
var indexCmd = &cobra.Command{
    Use:   "index",
    Short: "索引管理",
}
 
var indexBuildCmd = &cobra.Command{
    Use:   "build",
    Short: "构建或重建知识库索引",
    Long:  `kb-cli index build [--vault=<路径>] [--db=<路径>] # 构建或重建知识库索引`,
    RunE:  runIndexBuild,
}
 
var indexStatusCmd = &cobra.Command{
    Use:   "status",
    Short: "查看索引状态信息",
    Long:  `kb-cli index status [--vault=<路径>] [--db=<路径>] # 查看索引状态(节点数、边数、commit、构建时间)`,
    RunE:  runIndexStatus,
}
 
var indexGcCmd = &cobra.Command{
    Use:   "gc",
    Short: "清理孤立节点和边",
    Long:  `kb-cli index gc [--vault=<路径>] [--db=<路径>] # 清理索引中文件已删除的孤立节点和边`,
    RunE:  runIndexGc,
}
 
var gitCmd = &cobra.Command{
    Use:   "git",
    Short: "Git 同步操作",
}
 
var gitSyncCmd = &cobra.Command{
    Use:   "sync",
    Short: "同步知识库到 Git 仓库",
    Long:  `kb-cli git sync [--vault=<路径>] # 执行 git add、commit、push 同步知识库`,
    RunE:  runGitSync,
}
 
func init() {
    rootCmd.AddCommand(indexCmd)
    indexCmd.AddCommand(indexBuildCmd)
    indexCmd.AddCommand(indexStatusCmd)
    indexCmd.AddCommand(indexGcCmd)
    
    rootCmd.AddCommand(gitCmd)
    gitCmd.AddCommand(gitSyncCmd)
}
 
func runIndexBuild(cmd *cobra.Command, args []string) error {
    // 打开索引
    store, err := index.Open(dbPath)
    if err != nil {
        return fmt.Errorf("打开索引失败: %w", err)
    }
    defer store.Close()
 
    // 获取当前 commit
    commit, err := index.GetGitCommit(vaultPath)
    if err != nil {
        fmt.Fprintln(os.Stderr, "警告: 无法获取 git commit:", err)
        commit = ""
    }
 
    fmt.Fprintln(os.Stderr, "正在重建索引...")
    if err := rebuildIndex(store, commit); err != nil {
        return fmt.Errorf("重建索引失败: %w", err)
    }
 
    fmt.Fprintln(os.Stderr, "索引重建完成")
    return nil
}
 
func runIndexStatus(cmd *cobra.Command, args []string) error {
    // 打开索引
    store, err := index.Open(dbPath)
    if err != nil {
        return fmt.Errorf("打开索引失败: %w", err)
    }
    defer store.Close()
 
    // 获取元信息
    gitCommit, err := store.GetMeta("git_commit")
    if err != nil {
        return fmt.Errorf("获取元信息失败: %w", err)
    }
    builtAt, _ := store.GetMeta("built_at")
 
    // 获取节点数
    nodeCount, err := store.NodeCount()
    if err != nil {
        return fmt.Errorf("获取节点数失败: %w", err)
    }
 
    // 获取边数
    edgeCount, err := store.EdgeCount()
    if err != nil {
        return fmt.Errorf("获取边数失败: %w", err)
    }
 
    // 获取当前 commit
    currentCommit, err := index.GetGitCommit(vaultPath)
    if err != nil {
        currentCommit = ""
    }
 
    // 输出状态
    fmt.Printf("索引状态:\n")
    fmt.Printf("  知识库路径: %s\n", vaultPath)
    fmt.Printf("  索引文件: %s\n", dbPath)
    fmt.Printf("  节点数: %d\n", nodeCount)
    fmt.Printf("  边数: %d\n", edgeCount)
    fmt.Printf("  索引 commit: %s\n", gitCommit)
    fmt.Printf("  当前 commit: %s\n", currentCommit)
    fmt.Printf("  构建时间: %s\n", builtAt)
 
    // 检查是否需要更新
    needsRebuild, _, err := index.NeedsRebuild(store, vaultPath)
    if err != nil {
        return fmt.Errorf("检查索引状态失败: %w", err)
    }
 
    if needsRebuild {
        fmt.Printf("  状态: 需要更新\n")
    } else {
        fmt.Printf("  状态: 最新\n")
    }
 
    return nil
}
 
func runIndexGc(cmd *cobra.Command, args []string) error {
    // 展开 ~ 为实际路径
    expandedVaultPath := vaultPath
    if strings.HasPrefix(vaultPath, "~/") {
        home, err := os.UserHomeDir()
        if err != nil {
            return fmt.Errorf("获取用户目录失败: %w", err)
        }
        expandedVaultPath = filepath.Join(home, vaultPath[2:])
    }
 
    // 打开索引
    store, err := index.Open(dbPath)
    if err != nil {
        return fmt.Errorf("打开索引失败: %w", err)
    }
    defer store.Close()
 
    fmt.Fprintln(os.Stderr, "正在清理孤立节点和边...")
 
    // 获取所有节点路径
    nodes, err := store.GetAllNodes()
    if err != nil {
        return fmt.Errorf("获取节点失败: %w", err)
    }
 
    // 检查文件是否存在
    var orphanPaths []string
    for _, node := range nodes {
        fullPath := filepath.Join(expandedVaultPath, node.Path)
        if _, err := os.Stat(fullPath); os.IsNotExist(err) {
            orphanPaths = append(orphanPaths, node.Path)
        }
    }
 
    if len(orphanPaths) == 0 {
        fmt.Fprintln(os.Stderr, "没有发现孤立节点")
        return nil
    }
 
    fmt.Fprintf(os.Stderr, "发现 %d 个孤立节点,正在清理...\n", len(orphanPaths))
 
    // 删除孤立节点
    deletedCount, err := store.DeleteNodesByPaths(orphanPaths)
    if err != nil {
        return fmt.Errorf("删除节点失败: %w", err)
    }
 
    fmt.Fprintf(os.Stderr, "已清理 %d 个孤立节点\n", deletedCount)
    return nil
}
 
func runGitSync(cmd *cobra.Command, args []string) error {
    // 展开 ~ 为实际路径
    expandedVaultPath := vaultPath
    if strings.HasPrefix(vaultPath, "~/") {
        home, err := os.UserHomeDir()
        if err != nil {
            return fmt.Errorf("获取用户目录失败: %w", err)
        }
        expandedVaultPath = filepath.Join(home, vaultPath[2:])
    }
 
    fmt.Fprintln(os.Stderr, "正在同步知识库到 Git 仓库...")
 
    // 检查是否是 git 仓库
    gitDir := filepath.Join(expandedVaultPath, ".git")
    if _, err := os.Stat(gitDir); os.IsNotExist(err) {
        return fmt.Errorf("知识库目录不是 Git 仓库: %s", expandedVaultPath)
    }
 
    // 执行 git add -A
    addCmd := exec.Command("git", "-C", expandedVaultPath, "add", "-A")
    if output, err := addCmd.CombinedOutput(); err != nil {
        return fmt.Errorf("git add 失败: %w\n%s", err, output)
    }
 
    // 检查是否有变更
    statusCmd := exec.Command("git", "-C", expandedVaultPath, "status", "--porcelain")
    statusOutput, err := statusCmd.Output()
    if err != nil {
        return fmt.Errorf("git status 失败: %w", err)
    }
 
    if len(statusOutput) == 0 {
        fmt.Fprintln(os.Stderr, "没有变更需要提交")
        return nil
    }
 
    // 生成 commit 信息
    commitMsg := fmt.Sprintf("kb-cli: 自动同步 %s", time.Now().Format("2006-01-02 15:04:05"))
    commitCmd := exec.Command("git", "-C", expandedVaultPath, "commit", "-m", commitMsg)
    if output, err := commitCmd.CombinedOutput(); err != nil {
        return fmt.Errorf("git commit 失败: %w\n%s", err, output)
    }
 
    // 执行 git push
    pushCmd := exec.Command("git", "-C", expandedVaultPath, "push")
    if output, err := pushCmd.CombinedOutput(); err != nil {
        return fmt.Errorf("git push 失败: %w\n%s", err, output)
    }
 
    fmt.Fprintln(os.Stderr, "Git 同步完成")
    return nil
}