-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskills.go
147 lines (129 loc) · 3.17 KB
/
skills.go
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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
var skillSet = map[string]*skillInput{
"read-file": {
Skill: "read-file",
Description: "This skill allows you to read a file.",
Args: []string{
"file-name",
},
Run: func(args []string) (*skillOutput, error) {
filePath := args[0]
output := readFile(repoRoot + "/" + filePath)
return &skillOutput{
Output: output,
}, nil
},
},
"write-file": {
Skill: "write-file",
Description: "This skill writes to a file.",
Args: []string{
"file-name",
"file-content",
},
Run: func(args []string) (*skillOutput, error) {
filePath := args[0]
content := args[1]
writeFile(repoRoot+"/"+filePath, content)
return &skillOutput{
Output: "File written successfully.",
}, nil
},
},
"delete-file": {
Skill: "delete-file",
Description: "This skill deletes a file.",
Args: []string{
"file-name",
},
Run: func(args []string) (*skillOutput, error) {
filePath := args[0]
deleteFile(repoRoot + "/" + filePath)
return &skillOutput{
Output: "File deleted successfully.",
}, nil
},
},
"list-files": {
Skill: "list-files",
Description: "This skill lists the files.",
Args: []string{},
Run: func(args []string) (*skillOutput, error) {
output := getFolderStructure(repoRoot)
return &skillOutput{
Output: output,
}, nil
},
},
"done": {
Skill: "done",
Description: "This skill indicates that work is finished.",
Args: []string{},
Run: func(args []string) (*skillOutput, error) {
return nil, nil
},
},
}
func parseSkill(input string) (*skillInput, error) {
endPos := strings.Index(input, skillEndTag)
if endPos == -1 {
return nil, fmt.Errorf("skill tags not found. Input: %s", input)
}
skillStr := input[0:endPos]
fmt.Println("Extraced skill", skillStr)
var skill skillInput
err := json.Unmarshal([]byte(skillStr), &skill)
if err != nil {
return nil, err
}
return &skill, nil
}
func performSkill(skill *skillInput) (*skillOutput, error) {
fmt.Println("Working on skill", skill)
skillDefinition, exists := skillSet[skill.Skill]
if !exists {
return nil, fmt.Errorf("command %v not recognized", skillDefinition)
}
skill.Run = skillDefinition.Run
return skill.RunSkill()
}
func formatSkillOutput(skill *skillInput, output *skillOutput) string {
skillBytes, _ := json.MarshalIndent(skill, "", " ")
resultBytes, _ := json.MarshalIndent(output, "", " ")
return fmt.Sprintf(formatterResult, skillBytes, resultBytes)
}
func initSkill() *skillInput {
skill := *skillSet["list-files"]
skill.Description = ""
return &skill
}
func getFolderStructure(path string) string {
files, err := os.ReadDir(path)
if err != nil {
return err.Error()
}
fileNames := make([]string, 0)
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
return strings.Join(fileNames, ", ")
}
func readFile(path string) string {
content, err := os.ReadFile(path)
if err != nil {
return err.Error()
}
return string(content)
}
func writeFile(path string, content string) error {
return os.WriteFile(path, []byte(content), os.ModePerm)
}
func deleteFile(path string) {
_ = os.Remove(path)
}