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:
parent
f85612a4da
commit
65e26f4a69
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ollie/tools/codeintel/internal/codeintel"
|
||||
|
|
@ -36,6 +37,27 @@ func main() {
|
|||
|
||||
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 ---
|
||||
|
||||
func cmdOutline() {
|
||||
|
|
@ -164,9 +186,9 @@ func cmdDependencies() {
|
|||
|
||||
func cmdOverview() {
|
||||
var a struct {
|
||||
Path string `json:"path"`
|
||||
Offset int `json:"offset"`
|
||||
Limit int `json:"limit"`
|
||||
Path string `json:"path"`
|
||||
Offset flexInt `json:"offset"`
|
||||
Limit flexInt `json:"limit"`
|
||||
}
|
||||
if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil {
|
||||
fail(err)
|
||||
|
|
@ -179,19 +201,20 @@ func cmdOverview() {
|
|||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
start := a.Offset
|
||||
start := int(a.Offset)
|
||||
limit := int(a.Limit)
|
||||
if start < 0 {
|
||||
fail(fmt.Errorf("offset must be non-negative"))
|
||||
}
|
||||
if a.Limit < 0 {
|
||||
if limit < 0 {
|
||||
fail(fmt.Errorf("limit must be non-negative"))
|
||||
}
|
||||
if start >= len(files) {
|
||||
start = len(files)
|
||||
}
|
||||
end := len(files)
|
||||
if a.Limit > 0 && start+a.Limit < end {
|
||||
end = start + a.Limit
|
||||
if limit > 0 && start+limit < end {
|
||||
end = start + limit
|
||||
}
|
||||
fmt.Printf("# Codebase Overview\n\nPath: `%s`\n\n", root)
|
||||
if len(files) > 0 && (start > 0 || end < len(files)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue