MCPcopy Create free account
hub / github.com/GongShichen/LuminaCode / ApplyToolResultBudgetWithPreview

Function ApplyToolResultBudgetWithPreview

tools/budget.go:16–50  ·  view source on GitHub ↗
(rawOutput, toolUseID, sessionDir string, maxChars, previewChars int)

Source from the content-addressed store, hash-verified

14}
15
16func ApplyToolResultBudgetWithPreview(rawOutput, toolUseID, sessionDir string, maxChars, previewChars int) (string, error) {
17 return applyToolResultBudgetInDir(rawOutput, toolUseID, filepath.Join(sessionDir, "tool-results"), maxChars, previewChars)
18}
19
20func ApplyToolResultBudgetInDir(rawOutput, toolUseID, resultsDir string, maxChars int) (string, error) {
21 return applyToolResultBudgetInDir(rawOutput, toolUseID, resultsDir, maxChars, 2000)
22}
23
24func applyToolResultBudgetInDir(rawOutput, toolUseID, resultsDir string, maxChars, previewChars int) (string, error) {
25 if charLen(rawOutput) <= maxChars {
26 return rawOutput, nil
27 }
28
29 filePath := filepath.Join(resultsDir, toolUseID+".txt")
30 sizeLabel := formatSize(len([]byte(rawOutput)))
31
32 preview := firstChars(rawOutput, previewChars)
33 if charLen(rawOutput) > previewChars {
34 preview += "\n..."
35 }
36
37 if err := os.MkdirAll(resultsDir, 0o700); err != nil {
38 truncated := hardTruncate(rawOutput, maxChars)
39 return truncated + fmt.Sprintf("\n\n... [Warning: Failed to persist full output to disk due to error: %s]", err), nil
40 }
41 if err := os.WriteFile(filePath, []byte(rawOutput), 0o600); err != nil {
42 truncated := hardTruncate(rawOutput, maxChars)
43 return truncated + fmt.Sprintf("\n\n... [Warning: Failed to persist full output to disk due to error: %s]", err), nil
44 }
45
46 previewCount := previewChars
47 if total := charLen(rawOutput); total < previewCount {
48 previewCount = total
49 }
50 return fmt.Sprintf(
51 "<persisted-output>\nOutput too large (%s). Full output saved to: %s\n\nPreview (first %d characters):\n%s\n</persisted-output>",
52 sizeLabel,
53 filePath,

Callers 1

ApplyToolResultBudgetFunction · 0.85

Calls 4

charLenFunction · 0.85
formatSizeFunction · 0.85
firstCharsFunction · 0.85
hardTruncateFunction · 0.85

Tested by

no test coverage detected