-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtools_readdir.go
More file actions
184 lines (154 loc) · 5.11 KB
/
Copy pathtools_readdir.go
File metadata and controls
184 lines (154 loc) · 5.11 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
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package aiusechat
import (
"fmt"
"os"
"path/filepath"
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
"github.com/wavetermdev/waveterm/pkg/util/fileutil"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wavebase"
)
const ReadDirDefaultMaxEntries = 500
const ReadDirHardMaxEntries = 10000
type readDirParams struct {
Path string `json:"path"`
MaxEntries *int `json:"max_entries"`
}
func parseReadDirInput(input any) (*readDirParams, error) {
result := &readDirParams{}
if input == nil {
return nil, fmt.Errorf("input is required")
}
if err := utilfn.ReUnmarshal(result, input); err != nil {
return nil, fmt.Errorf("invalid input format: %w", err)
}
if result.Path == "" {
return nil, fmt.Errorf("missing path parameter")
}
if result.MaxEntries == nil {
maxEntries := ReadDirDefaultMaxEntries
result.MaxEntries = &maxEntries
}
if *result.MaxEntries < 1 {
return nil, fmt.Errorf("max_entries must be at least 1, got %d", *result.MaxEntries)
}
if *result.MaxEntries > ReadDirHardMaxEntries {
return nil, fmt.Errorf("max_entries cannot exceed %d, got %d", ReadDirHardMaxEntries, *result.MaxEntries)
}
return result, nil
}
func verifyReadDirInput(input any, toolUseData *uctypes.UIMessageDataToolUse) error {
params, err := parseReadDirInput(input)
if err != nil {
return err
}
expandedPath, err := wavebase.ExpandHomeDir(params.Path)
if err != nil {
return fmt.Errorf("failed to expand path: %w", err)
}
if !filepath.IsAbs(expandedPath) {
return fmt.Errorf("path must be absolute, got relative path: %s", params.Path)
}
fileInfo, err := os.Stat(expandedPath)
if err != nil {
return fmt.Errorf("failed to stat path: %w", err)
}
if !fileInfo.IsDir() {
return fmt.Errorf("path is not a directory, cannot be read with the read_dir tool. use the read_text_file tool if available to read files")
}
return nil
}
func readDirCallback(input any, toolUseData *uctypes.UIMessageDataToolUse) (any, error) {
params, err := parseReadDirInput(input)
if err != nil {
return nil, err
}
expandedPath, err := wavebase.ExpandHomeDir(params.Path)
if err != nil {
return nil, fmt.Errorf("failed to expand path: %w", err)
}
if !filepath.IsAbs(expandedPath) {
return nil, fmt.Errorf("path must be absolute, got relative path: %s", params.Path)
}
result, err := fileutil.ReadDir(params.Path, *params.MaxEntries)
if err != nil {
return nil, err
}
resultMap := map[string]any{
"path": result.Path,
"absolute_path": result.AbsolutePath,
"entry_count": result.EntryCount,
"total_entries": result.TotalEntries,
"entries": result.Entries,
}
if result.Truncated {
resultMap["truncated"] = true
resultMap["truncated_message"] = fmt.Sprintf("Directory listing truncated to %d entries (out of %d total). Increase max_entries to see more.", result.EntryCount, result.TotalEntries)
}
if result.ParentDir != "" {
resultMap["parent_dir"] = result.ParentDir
}
return resultMap, nil
}
func GetReadDirToolDefinition() uctypes.ToolDefinition {
return uctypes.ToolDefinition{
Name: "read_dir",
DisplayName: "Read Directory",
Description: "Read a directory from the filesystem and list its contents. Returns information about files and subdirectories including names, types, sizes, permissions, and modification times.",
ToolLogName: "gen:readdir",
Strict: false,
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"path": map[string]any{
"type": "string",
"description": "Absolute path to the directory to read. Supports '~' for the user's home directory. Relative paths are not supported.",
},
"max_entries": map[string]any{
"type": "integer",
"minimum": 1,
"maximum": 10000,
"default": 500,
"description": "Maximum number of entries to return. Defaults to 500, max 10000.",
},
},
"required": []string{"path"},
"additionalProperties": false,
},
ToolCallDesc: func(input any, output any, toolUseData *uctypes.UIMessageDataToolUse) string {
parsed, err := parseReadDirInput(input)
if err != nil {
return fmt.Sprintf("error parsing input: %v", err)
}
readFullDir := false
if output != nil {
if outputMap, ok := output.(map[string]any); ok {
_, wasTruncated := outputMap["truncated"]
readFullDir = !wasTruncated
}
}
if readFullDir {
return fmt.Sprintf("reading directory %q (entire directory)", parsed.Path)
}
return fmt.Sprintf("reading directory %q (max_entries: %d)", parsed.Path, *parsed.MaxEntries)
},
ToolAnyCallback: readDirCallback,
ToolApproval: func(input any) string {
parsed, err := parseReadDirInput(input)
if err != nil {
return uctypes.ApprovalNeedsApproval
}
expandedPath, err := wavebase.ExpandHomeDir(parsed.Path)
if err != nil {
return uctypes.ApprovalNeedsApproval
}
if IsSessionReadApproved(expandedPath) {
return uctypes.ApprovalAutoApproved
}
return uctypes.ApprovalNeedsApproval
},
ToolVerifyInput: verifyReadDirInput,
}
}