Files
ARTEX/third_party/norma/tool/helpers.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

105 lines
3.4 KiB
Go

package tool
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
)
const defaultMaxOutput = 30000
func maxOut(tc *ToolContext) int {
if tc != nil && tc.MaxOutputChars > 0 {
return tc.MaxOutputChars
}
return defaultMaxOutput
}
var spillSeq atomic.Int64
// Capture bounds a tool's textual output for the model. When the output exceeds
// the limit (ToolContext.MaxOutputChars, default 30000) and the host configured
// an OutputDir, the FULL output is written to a file there and a tight head + a
// pointer to that file is returned — so the model can read slices on demand
// (grep/sed) instead of losing the overflow. Without an OutputDir it falls back
// to a head+tail truncation (the overflow is discarded). Tools that produce
// potentially large output should return Capture(tc, output) rather than the raw
// string.
func Capture(tc *ToolContext, s string) string {
max := maxOut(tc)
if len(s) <= max {
return s
}
if tc != nil && tc.OutputDir != "" {
if path, err := spillOutput(tc.OutputDir, s); err == nil {
ref := path
if tc.WorkingDir != "" {
if rel, e := filepath.Rel(tc.WorkingDir, path); e == nil && !strings.HasPrefix(rel, "..") {
ref = rel
}
}
lines := strings.Count(s, "\n") + 1
return s[:max] + fmt.Sprintf(
"\n\n... <persisted-output>[Output too large: full %d bytes / %d lines.Full output saved to %s </persisted-output>",
len(s), lines, ref)
}
}
return truncate(s, max)
}
// alreadyCaptured reports whether s already carries a marker Capture leaves
// behind — a spill pointer or a head+tail truncation notice. It is how a global
// post-tool net recognises output a tool already ran through Capture itself.
//
// A false negative (legitimate text that happens to contain a marker) only means
// the guard fails to re-cap already-large output; it never corrupts. The markers
// are distinctive enough that this is vanishingly rare.
func alreadyCaptured(s string) bool {
return strings.Contains(s, "<persisted-output>") || strings.Contains(s, "characters truncated]")
}
// CaptureOnce is Capture with an idempotency guard: output a tool already ran
// through Capture (so it carries a spill pointer or truncation notice) is
// returned unchanged. This lets a single post-tool net cap every tool's output
// without re-truncating or re-spilling what built-in / MCP / custom tools
// already handled themselves. Output within the limit is a no-op either way,
// since Capture returns it unchanged.
func CaptureOnce(tc *ToolContext, s string) string {
if alreadyCaptured(s) {
return s
}
return Capture(tc, s)
}
func spillOutput(dir, s string) (string, error) {
if err := os.MkdirAll(dir, 0o755); err != nil {
return "", err
}
name := fmt.Sprintf("output-%d-%d.txt", time.Now().Unix(), spillSeq.Add(1))
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(s), 0o644); err != nil {
return "", err
}
return path, nil
}
// resolvePath joins a relative path against the working directory.
func resolvePath(tc *ToolContext, p string) string {
if filepath.IsAbs(p) || tc == nil || tc.WorkingDir == "" {
return p
}
return filepath.Join(tc.WorkingDir, p)
}
// truncate trims s to max characters, keeping head and tail.
func truncate(s string, max int) string {
if max <= 0 || len(s) <= max {
return s
}
half := max / 2
return s[:half] + fmt.Sprintf("\n\n... [%d characters truncated] ...\n\n", len(s)-max) + s[len(s)-half:]
}