Make file read integer arguments explicit

This commit is contained in:
Ollie Agent 2026-08-18 08:07:59 +02:00
parent 76eb5bea99
commit d90bd77fa0
2 changed files with 27 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"description": "Read a file with line numbers. Prefer segments over full reads.", "description": "Read a file with line numbers. Prefer segments over full reads.",
"prompt": "## file_read\n\nRead a file with line numbers. Prefer segments over full reads.\n\n**Args**: `path` (required), `start`, `end`, or `head`\n\n- `start`/`end`: 1-indexed line numbers, inclusive. Must be strings. Max segment: 2000 lines.\n- No start/end: reads up to 500 lines from line 1.\n- `head`: read first N lines (alternative to start/end).\n\n```\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\")\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\", start=\"100\", end=\"200\")\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\", head=\"50\")\n```\n\n- `start`/`end` are strings even though they represent numbers.\n- Use `head` as alternative to `start`/`end`.\n\n**Constraints**: Absolute paths only.", "prompt": "## file_read\n\nRead a file with line numbers. Prefer segments over full reads.\n\n**Args**: `path` (required), `start`, `end`, or `head`\n\n- `start`/`end`: 1-indexed line numbers, inclusive. Use integers. Max segment: 2000 lines.\n- No start/end: reads up to 500 lines from line 1.\n- `head`: read first N lines (alternative to start/end).\n\n```\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\")\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\", start=100, end=200)\nfile_read(path=\"<ABSOLUTE_PATH_FROM_PWD>\", head=50)\n```\n\n- Use `start`/`end` as integers even though they represent line numbers.\n- Use `head` as an integer alternative to `start`/`end`.\n\n**Constraints**: Absolute paths only.",
"args": { "args": {
"type": "object", "type": "object",
"required": [ "required": [
@ -12,16 +12,16 @@
"description": "Absolute path to file" "description": "Absolute path to file"
}, },
"start": { "start": {
"type": "string", "type": "integer",
"description": "1-indexed start line (default: 1)" "description": "1-indexed start line (default: 1). Use an integer."
}, },
"end": { "end": {
"type": "string", "type": "integer",
"description": "1-indexed end line, inclusive (max 2000 line segment)" "description": "1-indexed end line, inclusive (max 2000 line segment). Use an integer."
}, },
"head": { "head": {
"type": "string", "type": "integer",
"description": "Read first N lines (alternative to start/end)" "description": "Read first N lines (alternative to start/end). Use an integer."
} }
} }
}, },

View File

@ -2,6 +2,7 @@ package filetools
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
@ -35,19 +36,31 @@ func Int(m map[string]any, key string, def *int) (*int, error) {
if !ok || v == nil { if !ok || v == nil {
return def, nil return def, nil
} }
var s string var n int
switch x := v.(type) { switch x := v.(type) {
case string: case string:
s = x s := strings.TrimSpace(x)
parsed, e := strconv.Atoi(s)
if e != nil {
return nil, fmt.Errorf("argument %q must be a valid integer, got %q", key, s)
}
n = parsed
case float64: case float64:
s = strconv.Itoa(int(x)) if x != float64(int(x)) {
return nil, fmt.Errorf("argument %q must be an integer, got %v", key, x)
}
n = int(x)
case json.Number:
parsed, e := strconv.Atoi(string(x))
if e != nil {
return nil, fmt.Errorf("argument %q must be a valid integer, got %q", key, x)
}
n = parsed
case int:
n = x
default: default:
return nil, fmt.Errorf("argument %q must be an integer, got %T", key, v) return nil, fmt.Errorf("argument %q must be an integer, got %T", key, v)
} }
n, e := strconv.Atoi(s)
if e != nil {
return nil, fmt.Errorf("argument %q must be a valid integer, got %q", key, s)
}
return &n, nil return &n, nil
} }
func Bool(m map[string]any, key string) (bool, error) { func Bool(m map[string]any, key string) (bool, error) {