Files
LaodingBot/internal/knowledge/loader.go

64 lines
1.3 KiB
Go
Raw Normal View History

2026-02-21 23:01:39 +08:00
package knowledge
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
func LoadSoul(path string) (string, error) {
b, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read soul file failed: %w", err)
}
content := strings.TrimSpace(string(b))
if content == "" {
return "", fmt.Errorf("soul file is empty")
}
return content, nil
}
func LoadSkills(dir string) (string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("read skills dir failed: %w", err)
}
files := make([]string, 0)
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
if strings.HasSuffix(strings.ToLower(name), ".md") {
files = append(files, filepath.Join(dir, name))
}
}
sort.Strings(files)
builder := strings.Builder{}
for _, file := range files {
b, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("read skill file failed: %w", err)
}
content := strings.TrimSpace(string(b))
if content == "" {
continue
}
builder.WriteString("## ")
builder.WriteString(filepath.Base(file))
builder.WriteString("\n")
builder.WriteString(content)
builder.WriteString("\n\n")
}
out := strings.TrimSpace(builder.String())
if out == "" {
return "", fmt.Errorf("no non-empty markdown skills loaded from %s", dir)
}
return out, nil
}