|
122 | 122 | {:dir (str d) :prefix prefix :hash hash})))) |
123 | 123 | [])) |
124 | 124 |
|
125 | | -(defn resolve-paths [dirs] |
| 125 | +(defn index-workspaces |
| 126 | + "Workspace folders ECA recorded in the cache dir's chat index (written by |
| 127 | + every client, so this also covers sessions started from emacs/neovim in |
| 128 | + folders outside our search roots). Nil when unreadable." |
| 129 | + [dir] |
| 130 | + (try |
| 131 | + (let [idx (json/parse-string (slurp (str (fs/path dir "chats" "index.transit.json")))) |
| 132 | + ;; transit-JSON map: ["^ " k1 v1 k2 v2 ...] |
| 133 | + ws (when (and (sequential? idx) (= "^ " (first idx))) |
| 134 | + (->> (rest idx) (partition 2) (some (fn [[k v]] (when (= k "~:workspaces") v)))))] |
| 135 | + (when (and (sequential? ws) (every? string? ws)) (vec ws))) |
| 136 | + (catch Exception _ nil))) |
| 137 | + |
| 138 | +(defn resolve-by-hash [dirs] |
126 | 139 | (let [wanted (set (map :prefix dirs)) |
127 | 140 | by-hash (->> (candidate-dirs) |
128 | 141 | (filter #(contains? wanted (sanitize-name (fs/file-name %)))) |
129 | 142 | (map #(str (fs/absolutize %))) |
130 | 143 | (reduce (fn [m p] (assoc m (paths-hash [p]) p)) {}))] |
131 | 144 | (keep #(when-let [path (get by-hash (:hash %))] (assoc % :path path)) dirs))) |
132 | 145 |
|
| 146 | +(defn resolve-paths |
| 147 | + "Maps cache dirs to folders. The chat index is authoritative; hashing |
| 148 | + candidate folders is the fallback. Multi-root workspaces are skipped since |
| 149 | + a session is opened on a single folder." |
| 150 | + [dirs] |
| 151 | + (let [from-index (keep (fn [d] |
| 152 | + (let [ws (index-workspaces (:dir d))] |
| 153 | + (when (and (= 1 (count ws)) (fs/directory? (first ws))) |
| 154 | + (assoc d :path (first ws))))) |
| 155 | + dirs) |
| 156 | + done (set (map :dir from-index))] |
| 157 | + (concat from-index (resolve-by-hash (remove #(done (:dir %)) dirs))))) |
| 158 | + |
133 | 159 | (defn history [eca dir] |
134 | 160 | (if-not eca |
135 | 161 | {:chatCount 0} |
|
0 commit comments