35 lines
932 B
Go
35 lines
932 B
Go
package sidekick
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// BufferGate checks if a result is too large. If it is, it writes it to a temp file
|
|
// and returns a pointer message instead. The path is added to tempFiles for later cleanup.
|
|
func BufferGate(result string, tempFiles *[]string, threshold int) (string, error) {
|
|
if len(result) <= threshold {
|
|
return result, nil
|
|
}
|
|
|
|
tmpFile, err := os.CreateTemp("", "sidekick-buf-*.txt")
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to create temp file for buffering: %w", err)
|
|
}
|
|
|
|
_, err = tmpFile.WriteString(result)
|
|
if err != nil {
|
|
tmpFile.Close()
|
|
return "", fmt.Errorf("failed to write to temp file: %w", err)
|
|
}
|
|
tmpFile.Close()
|
|
|
|
*tempFiles = append(*tempFiles, tmpFile.Name())
|
|
|
|
msg := fmt.Sprintf(
|
|
"Tool result is available in file '%s'. File size: %d bytes. Use read_file or grep_file to access it.",
|
|
tmpFile.Name(), len(result),
|
|
)
|
|
return msg, nil
|
|
}
|