-
-
Notifications
You must be signed in to change notification settings - Fork 36
Optimize large shell tool output #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
bobrowadam
wants to merge
2
commits into
editor-code-assistant:master
from
bobrowadam:optimize-large-shell-output
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,15 @@ | |
| (:require | ||
| [babashka.fs :as fs] | ||
| [babashka.process :as p] | ||
| [clojure.java.io :as io] | ||
| [clojure.string :as string] | ||
| [eca.config :as config] | ||
| [eca.features.tools.util :as tools.util] | ||
| [eca.logger :as logger] | ||
| [eca.shared :as shared])) | ||
| [eca.shared :as shared]) | ||
| (:import | ||
| [java.security MessageDigest] | ||
| [java.util Base64])) | ||
|
|
||
| (set! *warn-on-reflection* true) | ||
|
|
||
|
|
@@ -49,7 +53,62 @@ | |
| :continue true} | ||
| input (assoc :in input))))) | ||
|
|
||
| (defn ^:private shell-command [arguments {:keys [db tool-call-id call-state-fn state-transition-fn]}] | ||
| (defn ^:private workspaces-hash | ||
| "Return an 8-char base64 (URL-safe, no padding) key for the given workspace set." | ||
| [workspaces] | ||
| (let [paths (->> workspaces | ||
| (map #(str (fs/absolutize (fs/file (shared/uri->filename (:uri %)))))) | ||
| (distinct) | ||
| (sort)) | ||
| joined (string/join ":" paths) | ||
| md (MessageDigest/getInstance "SHA-256") | ||
| digest (.digest (doto md (.update (.getBytes ^String joined "UTF-8")))) | ||
| encoder (-> (Base64/getUrlEncoder) | ||
| (.withoutPadding)) | ||
| key (.encodeToString encoder digest)] | ||
| (subs key 0 (min 8 (count key))))) | ||
|
|
||
| (defn ^:private shell-output-cache-dir | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this functoin is better to live in eca.cache along with the |
||
| "Get the directory path for storing shell output files. | ||
| Returns: ~/.cache/eca/{workspace-hash}/shell-output/" | ||
| [workspaces] | ||
| (let [ws-hash (workspaces-hash workspaces)] | ||
| (io/file (tools.util/eca-cache-base-dir) ws-hash "shell-output"))) | ||
|
|
||
| (defn ^:private write-output-to-file | ||
| "Write shell command output to a file in the cache directory. | ||
| Returns the file path on success, or nil on failure." | ||
| [output chat-id tool-call-id workspaces] | ||
| (try | ||
| (let [dir (shell-output-cache-dir workspaces) | ||
| filename (str chat-id "-" tool-call-id ".txt") | ||
| file-path (io/file dir filename)] | ||
| (io/make-parents file-path) | ||
| (spit file-path output) | ||
| (str file-path)) | ||
| (catch Exception e | ||
| (logger/warn logger-tag "Failed to write output to file" | ||
| {:chat-id chat-id | ||
| :tool-call-id tool-call-id | ||
| :error (.getMessage e)}) | ||
| nil))) | ||
|
|
||
| (defn ^:private format-file-based-response | ||
| ([file-path output exit-code file-size] | ||
| (format-file-based-response file-path output exit-code file-size 20)) | ||
| ([file-path output exit-code file-size tail] | ||
| (let [lines (string/split-lines output) | ||
| tail-lines (take-last tail lines) | ||
| tail-str (string/join "\n" tail-lines) | ||
| formatted-size (format "%,d" file-size)] | ||
| (str "Shell command output saved to file due to size, you should inspect the file if needed.\n\n" | ||
| "File: " file-path "\n" | ||
| "Size: " formatted-size " characters\n" | ||
| "Exit code: " exit-code "\n\n" | ||
| (format "Last %s lines:\n" (count tail-lines)) | ||
| tail-str)))) | ||
|
|
||
| (defn ^:private shell-command [arguments {:keys [db tool-call-id chat-id config call-state-fn state-transition-fn]}] | ||
| (let [command-args (get arguments "command") | ||
| user-work-dir (get arguments "working_directory") | ||
| timeout (min (or (get arguments "timeout") default-timeout) max-timeout)] | ||
|
|
@@ -77,7 +136,7 @@ | |
| (tools.util/tool-call-destroy-resource! "eca__shell_command" :process proc) | ||
| (state-transition-fn :resources-destroyed {:resources [:process]}) | ||
| {:exit 1 :err msg})))) | ||
| {:exit 1 :err "Tool call is :stopping, so shell rpocess not spawned"}) | ||
| {:exit 1 :err "Tool call is :stopping, so shell process not spawned"}) | ||
| (catch Exception e | ||
| ;; Process did not start, or had an Exception (other than InterruptedException) during execution. | ||
| (let [msg (or (.getMessage e) "Caught an Exception during execution of the shell tool")] | ||
|
|
@@ -93,22 +152,109 @@ | |
| (state-transition-fn :resources-destroyed {:resources (keys resources)})))))) | ||
| err (some-> (:err result) string/trim) | ||
| out (some-> (:out result) string/trim)] | ||
| (if (= result ::timeout) | ||
| (cond | ||
| (= result ::timeout) | ||
| (do | ||
| (logger/debug logger-tag "Command timed out after " timeout " ms") | ||
| (tools.util/single-text-content (str "Command timed out after " timeout " ms") true)) | ||
| (do | ||
|
|
||
| (zero? (:exit result)) | ||
| (let [output (or out "") | ||
| output-size (count output) | ||
| threshold (get-in config [:toolCall :shellCommand :outputFileThreshold] 1000)] | ||
| (logger/debug logger-tag "Command executed:" result) | ||
| (if (> output-size threshold) | ||
| ;; Large output: write to file and return summary | ||
| (if (and chat-id tool-call-id) | ||
| (if-let [file-path (write-output-to-file output chat-id tool-call-id (:workspace-folders db))] | ||
| (let [response (tools.util/single-text-content | ||
| (format-file-based-response file-path output (:exit result) output-size))] | ||
| (logger/debug logger-tag "Returning file-based response") | ||
| response) | ||
| ;; Fallback: if file write failed, inline the output with a warning | ||
| (do | ||
| (logger/warn logger-tag "File write failed, falling back to inline output") | ||
| (tools.util/single-text-content | ||
| (str "Warning: Failed to write output to file, showing inline instead.\n\n" | ||
| output)))) | ||
| ;; chat-id or tool-call-id is nil, inline the output | ||
| (do | ||
| (logger/warn logger-tag "chat-id or tool-call-id is nil, inlining large output" | ||
| {:chat-id chat-id :tool-call-id tool-call-id}) | ||
| (tools.util/single-text-content output))) | ||
| ;; Small output: inline with structured response | ||
| {:error false | ||
| :contents (remove nil? | ||
| (concat [{:type :text | ||
| :text (str "Exit code: " (:exit result))}] | ||
| (when-not (string/blank? err) | ||
| [{:type :text | ||
| :text (str "Stderr:\n" err)}]) | ||
| (when-not (string/blank? out) | ||
| [{:type :text | ||
| :text (str "Stdout:\n" out)}])))})) | ||
|
|
||
| :else | ||
| (let [output (str (when-not (string/blank? out) out) | ||
| (when (and (not (string/blank? out)) | ||
| (not (string/blank? err))) | ||
| "\n") | ||
| (when-not (string/blank? err) err)) | ||
| output-size (count output) | ||
| threshold (get-in config [:toolCall :shellCommand :outputFileThreshold] 1000)] | ||
| (logger/debug logger-tag "Command executed:" result) | ||
| {:error (not (zero? (:exit result))) | ||
| :contents (remove nil? | ||
| (concat [{:type :text | ||
| :text (str "Exit code: " (:exit result))}] | ||
| (when-not (string/blank? err) | ||
| [{:type :text | ||
| :text (str "Stderr:\n" err)}]) | ||
| (when-not (string/blank? out) | ||
| [{:type :text | ||
| :text (str "Stdout:\n" out)}])))})))))) | ||
| (if (> output-size threshold) | ||
| ;; Large error output: write to file and return summary | ||
| (if (and chat-id tool-call-id) | ||
| (if-let [file-path (write-output-to-file output chat-id tool-call-id (:workspace-folders db))] | ||
| (let [response-text (str "Command failed with exit code " (:exit result) ".\n\n" | ||
| "Output saved to file due to size: " file-path "\n" | ||
| "Size: " (format "%,d" output-size) " characters\n\n" | ||
| (format "Last 20 lines:\n") | ||
| (string/join "\n" (take-last 20 (string/split-lines output))))] | ||
| (logger/debug logger-tag "Returning file-based error response") | ||
| {:error (not (zero? (:exit result))) | ||
| :contents [{:type :text | ||
| :text response-text}]}) | ||
| ;; Fallback: if file write failed, inline the output with a warning | ||
| (do | ||
| (logger/warn logger-tag "File write failed for error output, falling back to inline") | ||
| {:error (not (zero? (:exit result))) | ||
| :contents (remove nil? | ||
| (concat [{:type :text | ||
| :text (str "Warning: Failed to write output to file, showing inline instead.\n\n" | ||
| "Exit code: " (:exit result))}] | ||
| (when-not (string/blank? err) | ||
| [{:type :text | ||
| :text (str "Stderr:\n" err)}]) | ||
| (when-not (string/blank? out) | ||
| [{:type :text | ||
| :text (str "Stdout:\n" out)}])))})) | ||
| ;; chat-id or tool-call-id is nil, inline the output | ||
| (do | ||
| (logger/warn logger-tag "chat-id or tool-call-id is nil, inlining large error output" | ||
| {:chat-id chat-id :tool-call-id tool-call-id}) | ||
| {:error (not (zero? (:exit result))) | ||
| :contents (remove nil? | ||
| (concat [{:type :text | ||
| :text (str "Exit code: " (:exit result))}] | ||
| (when-not (string/blank? err) | ||
| [{:type :text | ||
| :text (str "Stderr:\n" err)}]) | ||
| (when-not (string/blank? out) | ||
| [{:type :text | ||
| :text (str "Stdout:\n" out)}])))})) | ||
| ;; Small error output: inline as before | ||
| {:error (not (zero? (:exit result))) | ||
| :contents (remove nil? | ||
| (concat [{:type :text | ||
| :text (str "Exit code: " (:exit result))}] | ||
| (when-not (string/blank? err) | ||
| [{:type :text | ||
| :text (str "Stderr:\n" err)}]) | ||
| (when-not (string/blank? out) | ||
| [{:type :text | ||
| :text (str "Stdout:\n" out)}])))}))))))) | ||
|
|
||
| (defn shell-command-summary [{:keys [args config]}] | ||
| (let [max-length (get-in config [:toolCall :shellCommand :summaryMaxLength])] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we duplicating this function if it exists in
eca.cache/workspaces-hash?