|
109 | 109 | (-> (io/resource (str "prompts/tools/" tool-name ".md")) |
110 | 110 | (slurp))) |
111 | 111 |
|
| 112 | +(defn reorder-schema-required-first |
| 113 | + "Returns schema as an ordered map with `type` first, `required` second when |
| 114 | + present, and the remaining entries in their existing iteration order." |
| 115 | + [{:keys [type required] :as schema}] |
| 116 | + (let [first-entries (cond-> [] |
| 117 | + type (conj [:type type]) |
| 118 | + required (conj [:required required]))] |
| 119 | + (if (seq first-entries) |
| 120 | + (into (apply array-map (mapcat identity first-entries)) |
| 121 | + (remove (fn [[k _]] (contains? #{:type :required} k)) schema)) |
| 122 | + schema))) |
| 123 | + |
112 | 124 | (defn required-params-error |
113 | 125 | "Given a tool `parameters` JSON schema (object) and an args map, return a |
114 | 126 | single-text-content error when any required parameter is missing. Returns nil |
|
123 | 135 | (->> missing (map #(str "`" % "`")) (string/join ", "))) |
124 | 136 | :error))))) |
125 | 137 |
|
| 138 | +(defn omit-optional-empty-string-args |
| 139 | + "Drops optional tool arguments whose value is the empty string. |
| 140 | + Required arguments are preserved exactly as provided." |
| 141 | + [parameters args] |
| 142 | + (let [required (->> (:required parameters) |
| 143 | + (map name) |
| 144 | + set)] |
| 145 | + (into {} |
| 146 | + (remove (fn [[k v]] |
| 147 | + (and (= "" v) |
| 148 | + (not (contains? required (name k)))))) |
| 149 | + args))) |
| 150 | + |
126 | 151 | (defn ^:private contents->text |
127 | 152 | "Concatenates all text contents from a tool result's :contents into a single string." |
128 | 153 | [contents] |
|
202 | 227 | (assoc result :contents [{:type :text |
203 | 228 | :text (str truncated notice)}])) |
204 | 229 | result))))) |
205 | | - |
206 | | -(defn normalize-optional-string |
207 | | - [value] |
208 | | - (some-> value string/trim not-empty)) |
|
0 commit comments