(rawOutput, toolUseID, sessionDir string, maxChars, previewChars int)
| 14 | } |
| 15 | |
| 16 | func ApplyToolResultBudgetWithPreview(rawOutput, toolUseID, sessionDir string, maxChars, previewChars int) (string, error) { |
| 17 | return applyToolResultBudgetInDir(rawOutput, toolUseID, filepath.Join(sessionDir, "tool-results"), maxChars, previewChars) |
| 18 | } |
| 19 | |
| 20 | func ApplyToolResultBudgetInDir(rawOutput, toolUseID, resultsDir string, maxChars int) (string, error) { |
| 21 | return applyToolResultBudgetInDir(rawOutput, toolUseID, resultsDir, maxChars, 2000) |
| 22 | } |
| 23 | |
| 24 | func 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, |
no test coverage detected