Add go-llm v2: redesigned API for simpler LLM abstraction
v2 is a new Go module (v2/) with a dramatically simpler API: - Unified Message type (no more Input marker interface) - Define[T] for ergonomic tool creation with standard context.Context - Chat session with automatic tool-call loop (agent loop) - Streaming via pull-based StreamReader - MCP one-call connect (MCPStdioServer, MCPHTTPServer, MCPSSEServer) - Middleware support (logging, retry, timeout, usage tracking) - Decoupled JSON Schema (map[string]any, no provider coupling) - Sample tools: WebSearch, Browser, Exec, ReadFile, WriteFile, HTTP - Providers: OpenAI, Anthropic, Google (all with streaming) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
81
v2/tools/readfile.go
Normal file
81
v2/tools/readfile.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
llm "gitea.stevedudenhoeffer.com/steve/go-llm/v2"
|
||||
)
|
||||
|
||||
// ReadFileParams defines parameters for the read file tool.
|
||||
type ReadFileParams struct {
|
||||
Path string `json:"path" description:"File path to read"`
|
||||
Start *int `json:"start,omitempty" description:"Starting line number (1-based, inclusive)"`
|
||||
End *int `json:"end,omitempty" description:"Ending line number (1-based, inclusive)"`
|
||||
}
|
||||
|
||||
// ReadFile creates a file reading tool.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// tools := llm.NewToolBox(tools.ReadFile())
|
||||
func ReadFile() llm.Tool {
|
||||
return llm.Define[ReadFileParams]("read_file", "Read the contents of a file",
|
||||
func(ctx context.Context, p ReadFileParams) (string, error) {
|
||||
f, err := os.Open(p.Path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("opening file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// If no line range specified, read the whole file (limited to 1MB)
|
||||
if p.Start == nil && p.End == nil {
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("stat file: %w", err)
|
||||
}
|
||||
if info.Size() > 1<<20 {
|
||||
return "", fmt.Errorf("file too large (%d bytes), use start/end to read a range", info.Size())
|
||||
}
|
||||
data, err := os.ReadFile(p.Path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("reading file: %w", err)
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// Read specific line range
|
||||
start := 1
|
||||
end := -1
|
||||
if p.Start != nil {
|
||||
start = *p.Start
|
||||
}
|
||||
if p.End != nil {
|
||||
end = *p.End
|
||||
}
|
||||
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(f)
|
||||
lineNum := 0
|
||||
for scanner.Scan() {
|
||||
lineNum++
|
||||
if lineNum < start {
|
||||
continue
|
||||
}
|
||||
if end > 0 && lineNum > end {
|
||||
break
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%d: %s", lineNum, scanner.Text()))
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "", fmt.Errorf("scanning file: %w", err)
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n"), nil
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user