-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuiltin_skills.go
More file actions
371 lines (315 loc) 路 8.98 KB
/
Copy pathbuiltin_skills.go
File metadata and controls
371 lines (315 loc) 路 8.98 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package toolfs
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
)
// BuiltinMemorySkill is the built-in memory skill that wraps InMemoryStore
type BuiltinMemorySkill struct {
store *InMemoryStore
}
// NewBuiltinMemorySkill creates a new built-in memory skill
func NewBuiltinMemorySkill(store *InMemoryStore) *BuiltinMemorySkill {
return &BuiltinMemorySkill{
store: store,
}
}
func (p *BuiltinMemorySkill) Name() string {
return "toolfs-memory"
}
func (p *BuiltinMemorySkill) Version() string {
return "1.0.0"
}
func (p *BuiltinMemorySkill) Init(config map[string]interface{}) error {
// Memory skill is already initialized with store
return nil
}
func (p *BuiltinMemorySkill) Execute(input []byte) ([]byte, error) {
var request SkillRequest
if err := json.Unmarshal(input, &request); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
switch request.Operation {
case "read_file", "read":
// Extract entry ID from path or data
entryID := p.extractEntryID(request.Path, request.Data)
if entryID == "" {
return json.Marshal(SkillResponse{
Success: false,
Error: "memory entry ID is required",
})
}
entry, err := p.store.Get(entryID)
if err != nil {
return json.Marshal(SkillResponse{
Success: false,
Error: err.Error(),
})
}
return json.Marshal(SkillResponse{
Success: true,
Result: entry,
})
case "write_file", "write":
entryID := p.extractEntryID(request.Path, request.Data)
if entryID == "" {
return json.Marshal(SkillResponse{
Success: false,
Error: "memory entry ID is required",
})
}
var content string
var metadata map[string]interface{}
// Try to parse content from data
if contentStr, ok := request.Data["content"].(string); ok {
content = contentStr
} else if inputStr, ok := request.Data["input"].(string); ok {
content = inputStr
}
// Parse metadata if available
if meta, ok := request.Data["metadata"].(map[string]interface{}); ok {
metadata = meta
}
// Try to parse from JSON input if content is structured
if content == "" && request.Data["input"] != nil {
if inputBytes, err := json.Marshal(request.Data["input"]); err == nil {
var entry MemoryEntry
if json.Unmarshal(inputBytes, &entry) == nil {
content = entry.Content
if entry.Metadata != nil {
metadata = entry.Metadata
}
}
}
}
err := p.store.Set(entryID, content, metadata)
if err != nil {
return json.Marshal(SkillResponse{
Success: false,
Error: err.Error(),
})
}
return json.Marshal(SkillResponse{
Success: true,
Result: map[string]interface{}{
"id": entryID,
"message": "memory entry written",
},
})
case "list_dir", "list":
entries, err := p.store.List()
if err != nil {
return json.Marshal(SkillResponse{
Success: false,
Error: err.Error(),
})
}
return json.Marshal(SkillResponse{
Success: true,
Result: map[string]interface{}{
"entries": entries,
},
})
default:
return json.Marshal(SkillResponse{
Success: false,
Error: fmt.Sprintf("unknown operation: %s", request.Operation),
})
}
}
func (p *BuiltinMemorySkill) extractEntryID(path string, data map[string]interface{}) string {
// Try to extract from path first
if path != "" {
parts := strings.Split(strings.Trim(path, "/"), "/")
for i, part := range parts {
if part == "memory" && i+1 < len(parts) {
return parts[i+1]
}
}
// If path doesn't contain "memory", use the last part
if len(parts) > 0 {
return parts[len(parts)-1]
}
}
// Try to extract from data
if data != nil {
if id, ok := data["entry_id"].(string); ok {
return id
}
if id, ok := data["id"].(string); ok {
return id
}
}
return ""
}
// GetSkillDocument implements SkillDocumentProvider
func (p *BuiltinMemorySkill) GetSkillDocument() string {
return `---
name: toolfs-memory
description: Persistent key-value storage for session data, conversation context, and agent state. Use this skill when the user requests storing or retrieving memory entries such as "Store this in memory", "Remember this preference", "Recall the previous conversation", or "List all memory entries".
metadata:
author: toolfs
version: "1.0.0"
module: memory
---
# ToolFS Memory
Persistent key-value storage for session data, conversation context, and agent state.
## Usage
### Read Memory Entry
GET /toolfs/memory/<entry_id>
### Write Memory Entry
PUT /toolfs/memory/<entry_id>
### List Memory Entries
LIST /toolfs/memory
`
}
// BuiltinRAGSkill is the built-in RAG skill that wraps InMemoryRAGStore
type BuiltinRAGSkill struct {
store *InMemoryRAGStore
}
// NewBuiltinRAGSkill creates a new built-in RAG skill
func NewBuiltinRAGSkill(store *InMemoryRAGStore) *BuiltinRAGSkill {
return &BuiltinRAGSkill{
store: store,
}
}
func (p *BuiltinRAGSkill) Name() string {
return "toolfs-rag"
}
func (p *BuiltinRAGSkill) Version() string {
return "1.0.0"
}
func (p *BuiltinRAGSkill) Init(config map[string]interface{}) error {
// RAG skill is already initialized with store
return nil
}
func (p *BuiltinRAGSkill) Execute(input []byte) ([]byte, error) {
var request SkillRequest
if err := json.Unmarshal(input, &request); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
switch request.Operation {
case "read_file", "read", "query", "search":
// Extract query from path or data
query, topK := p.extractQuery(request.Path, request.Data)
if query == "" {
return json.Marshal(SkillResponse{
Success: false,
Error: "query text is required",
})
}
results, err := p.store.Search(query, topK)
if err != nil {
return json.Marshal(SkillResponse{
Success: false,
Error: err.Error(),
})
}
return json.Marshal(SkillResponse{
Success: true,
Result: RAGSearchResults{
Query: query,
TopK: topK,
Results: results,
},
})
default:
return json.Marshal(SkillResponse{
Success: false,
Error: fmt.Sprintf("unknown operation: %s", request.Operation),
})
}
}
func (p *BuiltinRAGSkill) extractQuery(path string, data map[string]interface{}) (string, int) {
var query string
topK := 5
// Try to extract from path (e.g., /toolfs/rag/query?text=AI+agent&top_k=3)
if path != "" && strings.Contains(path, "query") {
parts := strings.SplitN(path, "?", 2)
if len(parts) == 2 {
queryURL, err := url.ParseQuery(parts[1])
if err == nil {
query = queryURL.Get("text")
if query == "" {
query = queryURL.Get("q")
}
if query != "" {
decoded, err := url.QueryUnescape(query)
if err == nil {
query = decoded
}
}
if topKStr := queryURL.Get("top_k"); topKStr != "" {
if k, err := strconv.Atoi(topKStr); err == nil && k > 0 {
topK = k
}
}
}
}
}
// Try to extract from data
if query == "" && data != nil {
if q, ok := data["query"].(string); ok {
query = q
} else if q, ok := data["text"].(string); ok {
query = q
}
if k, ok := data["top_k"].(float64); ok {
topK = int(k)
}
}
return query, topK
}
// GetSkillDocument implements SkillDocumentProvider
func (p *BuiltinRAGSkill) GetSkillDocument() string {
return `---
name: toolfs-rag
description: Semantic search over vector databases for document retrieval. Use this skill when the user requests searching documents, finding relevant content, or performing semantic queries such as "Search for information about X", "Find documents related to Y", or "Query the knowledge base".
metadata:
author: toolfs
version: "1.0.0"
module: rag
---
# ToolFS RAG
Semantic search over vector databases for document retrieval.
## Usage
### Semantic Search
GET /toolfs/rag/query?text=<query_text>&top_k=<number>
`
}
// BuiltinSkills holds references to all built-in skills
type BuiltinSkills struct {
Memory *BuiltinMemorySkill
RAG *BuiltinRAGSkill
}
// RegisterBuiltinSkills registers all built-in skills with the skill manager
// Only registers skills if the stores are the built-in implementations
func RegisterBuiltinSkills(fs *ToolFS, manager *SkillExecutorManager, session *Session) (*BuiltinSkills, error) {
ctx := NewSkillContext(fs, session)
var memorySkill *BuiltinMemorySkill
var ragSkill *BuiltinRAGSkill
// Create memory skill if using built-in store
if inMemoryStore, ok := fs.memoryStore.(*InMemoryStore); ok {
memorySkill = NewBuiltinMemorySkill(inMemoryStore)
if err := manager.InjectSkill(memorySkill, ctx, nil); err != nil {
return nil, fmt.Errorf("failed to register memory skill: %w", err)
}
}
// Create RAG skill if using built-in store
if inMemoryRAGStore, ok := fs.ragStore.(*InMemoryRAGStore); ok {
ragSkill = NewBuiltinRAGSkill(inMemoryRAGStore)
if err := manager.InjectSkill(ragSkill, ctx, nil); err != nil {
return nil, fmt.Errorf("failed to register RAG skill: %w", err)
}
}
// Return nil if neither skill could be registered
if memorySkill == nil && ragSkill == nil {
return nil, fmt.Errorf("built-in skills require built-in store implementations")
}
return &BuiltinSkills{
Memory: memorySkill,
RAG: ragSkill,
}, nil
}