codeintel: fix codebase_overview to accept string or int for offset/limit

LLMs sometimes send integer parameters as strings (e.g. "20" instead of 20).
Add flexInt type that unmarshals from either JSON int or string.
This commit is contained in:
Levi Neely 2026-10-02 09:32:36 +02:00
parent f85612a4da
commit 65e26f4a69
1 changed files with 30 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"ollie/tools/codeintel/internal/codeintel" "ollie/tools/codeintel/internal/codeintel"
@ -36,6 +37,27 @@ func main() {
func fail(e error) { fmt.Fprintln(os.Stderr, "error:", e); os.Exit(1) } func fail(e error) { fmt.Fprintln(os.Stderr, "error:", e); os.Exit(1) }
// flexInt unmarshals from JSON int or string.
type flexInt int
func (f *flexInt) UnmarshalJSON(data []byte) error {
var i int
if err := json.Unmarshal(data, &i); err == nil {
*f = flexInt(i)
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
n, err := strconv.Atoi(s)
if err != nil {
return err
}
*f = flexInt(n)
return nil
}
// --- code_outline --- // --- code_outline ---
func cmdOutline() { func cmdOutline() {
@ -164,9 +186,9 @@ func cmdDependencies() {
func cmdOverview() { func cmdOverview() {
var a struct { var a struct {
Path string `json:"path"` Path string `json:"path"`
Offset int `json:"offset"` Offset flexInt `json:"offset"`
Limit int `json:"limit"` Limit flexInt `json:"limit"`
} }
if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil { if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil {
fail(err) fail(err)
@ -179,19 +201,20 @@ func cmdOverview() {
if err != nil { if err != nil {
fail(err) fail(err)
} }
start := a.Offset start := int(a.Offset)
limit := int(a.Limit)
if start < 0 { if start < 0 {
fail(fmt.Errorf("offset must be non-negative")) fail(fmt.Errorf("offset must be non-negative"))
} }
if a.Limit < 0 { if limit < 0 {
fail(fmt.Errorf("limit must be non-negative")) fail(fmt.Errorf("limit must be non-negative"))
} }
if start >= len(files) { if start >= len(files) {
start = len(files) start = len(files)
} }
end := len(files) end := len(files)
if a.Limit > 0 && start+a.Limit < end { if limit > 0 && start+limit < end {
end = start + a.Limit end = start + limit
} }
fmt.Printf("# Codebase Overview\n\nPath: `%s`\n\n", root) fmt.Printf("# Codebase Overview\n\nPath: `%s`\n\n", root)
if len(files) > 0 && (start > 0 || end < len(files)) { if len(files) > 0 && (start > 0 || end < len(files)) {