|
148 | 148 | (or (> line-count max-lines) |
149 | 149 | (> size-kb max-size-kb)))) |
150 | 150 |
|
151 | | -(defn ^:private truncate-text-to-lines |
| 151 | +(defn ^:private truncate-text-lines |
152 | 152 | "Truncates text to the given number of lines." |
153 | 153 | [^String text max-lines] |
154 | | - (let [lines (string/split-lines text)] |
155 | | - (if (<= (count lines) max-lines) |
156 | | - text |
157 | | - (string/join "\n" (take max-lines lines))))) |
| 154 | + (->> text |
| 155 | + string/split-lines |
| 156 | + (take max-lines) |
| 157 | + (string/join "\n"))) |
| 158 | + |
| 159 | +(defn ^:private truncate-text-size |
| 160 | + "Truncates text to the given size in kb." |
| 161 | + [^String text max-size-kb] |
| 162 | + (->> text |
| 163 | + (#(.getBytes % "UTF-8")) |
| 164 | + (take (* max-size-kb 1024)) |
| 165 | + (byte-array) |
| 166 | + (#(String. % "UTF-8")))) |
| 167 | + |
| 168 | +(defn ^:private truncate-text |
| 169 | + "Truncates text to the given number of lines and size" |
| 170 | + [^String text max-lines max-size-kb] |
| 171 | + (-> text |
| 172 | + (truncate-text-lines max-lines) |
| 173 | + (truncate-text-size max-size-kb))) |
158 | 174 |
|
159 | 175 | (defn maybe-truncate-output |
160 | 176 | "Checks if a tool call result exceeds configured output truncation limits. |
|
171 | 187 | (let [full-text (str (contents->text (:contents result)))] |
172 | 188 | (if (exceeds-truncation-limits? full-text max-lines max-size-kb) |
173 | 189 | (let [saved-path (cache/save-tool-call-output! tool-call-id full-text) |
174 | | - truncated (truncate-text-to-lines full-text max-lines) |
| 190 | + truncated (truncate-text full-text max-lines max-size-kb) |
175 | 191 | notice (str "\n\n[OUTPUT TRUNCATED] The tool call succeeded but the output was truncated. " |
176 | 192 | "Full output saved to: " saved-path "\n" |
177 | 193 | "Use `eca__grep` or `eca__read_file` with offset/limit to view specific sections. Do not full read the file.")] |
|
0 commit comments