|
1 |
| -(ns init |
| 1 | +(ns init |
2 | 2 | (:require
|
| 3 | + [cheshire.core :as json] |
3 | 4 | [clojure.spec.alpha :as s]))
|
4 | 5 |
|
5 | 6 | ;; validate initial thought
|
|
12 | 13 | (s/def ::thoughtNumber number?)
|
13 | 14 | (s/def ::totalThoughts number?)
|
14 | 15 | (s/def ::nextThoughtNeeded boolean?)
|
15 |
| -(s/def ::thought-data (s/keys :req-un [::thought ::thoughtNumber ::totalThoughts ::nextThoughtNeeded])) |
| 16 | + |
| 17 | +(s/def ::isRevision boolean?) |
| 18 | +(s/def ::revisesThought number?) |
| 19 | +(s/def ::branchFromThought number?) |
| 20 | +(s/def ::branchId string?) |
| 21 | +(s/def ::needsMoreThoughts boolean?) |
| 22 | +(s/def ::thought-data (s/keys :req-un [::thought ::thoughtNumber ::totalThoughts ::nextThoughtNeeded] |
| 23 | + :opt-un [::isRevision ::revisesThought ::branchFromThought ::branchId ::needsMoreThoughts])) |
| 24 | + |
| 25 | +(defn process-thought |
| 26 | + [m] |
| 27 | + (if (not (s/valid? ::thought-data m)) |
| 28 | + (throw (ex-info "Invalid thought data" (s/explain-data ::thought-data m))) |
| 29 | + (let [thought-history (json/parse-string (slurp "thought-history.json") keyword) |
| 30 | + branches (json/parse-string (slurp "branches.json") keyword)] |
| 31 | + (spit "/sequentialthinking/thought-history.json" |
| 32 | + (json/generate-string |
| 33 | + (conj thought-history (cond-> m |
| 34 | + (> (:thoughtNumber m) (:totalThoughts m)) (assoc :totalThoughts (:thoughtNumber m)))))) |
| 35 | + (when (= (:branchFromThought m) (:branchId m)) |
| 36 | + (spit "/sequentialthinking/branches.json" |
| 37 | + (json/generate-string |
| 38 | + (update branches (:branchId m) (fnil conj []) m)))) |
| 39 | + (-> m |
| 40 | + (select-keys [:thoughtNumber :totalThoughts :nextThoughtNeeded]) |
| 41 | + (assoc :branches (keys branches)) |
| 42 | + (assoc :thoughtHistoryLength (count thought-history)))))) |
| 43 | + |
| 44 | +(defn -main [& args] |
| 45 | + (try |
| 46 | + (-> (json/parse-string (first args)) |
| 47 | + (process-thought) |
| 48 | + (json/generate-string) |
| 49 | + (println)) |
| 50 | + (catch Throwable t |
| 51 | + (println (.getMessage t)) |
| 52 | + (System/exit 1)))) |
| 53 | + |
| 54 | +(apply -main *command-line-args*) |
0 commit comments