chore: initial commit
This commit is contained in:
63
internal/knowledge/loader.go
Normal file
63
internal/knowledge/loader.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user