|
1 | 1 | (ns eca.features.context |
2 | 2 | (:require |
3 | 3 | [babashka.fs :as fs] |
| 4 | + [eca.config :as config] |
4 | 5 | [eca.features.index :as f.index] |
5 | 6 | [eca.features.tools.mcp :as f.mcp] |
6 | 7 | [eca.llm-api :as llm-api] |
|
11 | 12 |
|
12 | 13 | (def ^:private logger-tag "[CONTEXT]") |
13 | 14 |
|
14 | | -(defn raw-contexts->refined [contexts db] |
15 | | - (mapcat (fn [{:keys [type path lines-range uri]}] |
16 | | - (case (name type) |
17 | | - "file" [{:type :file |
18 | | - :path path |
19 | | - :partial (boolean lines-range) |
20 | | - :content (llm-api/refine-file-context path lines-range)}] |
21 | | - "directory" (->> (fs/glob path "**") |
22 | | - (remove fs/directory?) |
23 | | - (map (fn [path] |
24 | | - (let [filename (str (fs/canonicalize path))] |
25 | | - {:type :file |
26 | | - :path filename |
27 | | - :content (llm-api/refine-file-context filename nil)})))) |
28 | | - "repoMap" [{:type :repoMap}] |
29 | | - "mcpResource" (try |
30 | | - (mapv |
31 | | - (fn [{:keys [text]}] |
32 | | - {:type :mcpResource |
33 | | - :uri uri |
34 | | - :content text}) |
35 | | - (:contents (f.mcp/get-resource! uri db))) |
36 | | - (catch Exception e |
37 | | - (logger/warn logger-tag (format "Error getting MCP resource %s: %s" uri (.getMessage e))) |
38 | | - [])))) |
39 | | - contexts)) |
| 15 | +(defn ^:private agents-file-contexts |
| 16 | + "Search for AGENT.md file both in workspaceRoot and global config dir." |
| 17 | + [db config] |
| 18 | + (let [local-agent-files (keep (fn [{:keys [uri]}] |
| 19 | + (let [agent-file (fs/path (shared/uri->filename uri) (:agentFileRelativePath config))] |
| 20 | + (when (fs/readable? agent-file) |
| 21 | + (fs/canonicalize agent-file)))) |
| 22 | + (:workspace-folders db)) |
| 23 | + global-agent-file (let [agent-file (fs/path (config/global-config-dir) (:agentFileRelativePath config))] |
| 24 | + (when (fs/readable? agent-file) |
| 25 | + (fs/canonicalize agent-file)))] |
| 26 | + (mapv (fn [path] |
| 27 | + {:type :file |
| 28 | + :path (str path) |
| 29 | + :partial false |
| 30 | + :content (llm-api/refine-file-context (str path) nil)}) |
| 31 | + (concat local-agent-files |
| 32 | + (when global-agent-file [global-agent-file]))))) |
| 33 | + |
| 34 | +(defn raw-contexts->refined [contexts db config] |
| 35 | + (concat (agents-file-contexts db config) |
| 36 | + (mapcat (fn [{:keys [type path lines-range uri]}] |
| 37 | + (case (name type) |
| 38 | + "file" [{:type :file |
| 39 | + :path path |
| 40 | + :partial (boolean lines-range) |
| 41 | + :content (llm-api/refine-file-context path lines-range)}] |
| 42 | + "directory" (->> (fs/glob path "**") |
| 43 | + (remove fs/directory?) |
| 44 | + (map (fn [path] |
| 45 | + (let [filename (str (fs/canonicalize path))] |
| 46 | + {:type :file |
| 47 | + :path filename |
| 48 | + :content (llm-api/refine-file-context filename nil)})))) |
| 49 | + "repoMap" [{:type :repoMap}] |
| 50 | + "mcpResource" (try |
| 51 | + (mapv |
| 52 | + (fn [{:keys [text]}] |
| 53 | + {:type :mcpResource |
| 54 | + :uri uri |
| 55 | + :content text}) |
| 56 | + (:contents (f.mcp/get-resource! uri db))) |
| 57 | + (catch Exception e |
| 58 | + (logger/warn logger-tag (format "Error getting MCP resource %s: %s" uri (.getMessage e))) |
| 59 | + [])))) |
| 60 | + contexts))) |
40 | 61 |
|
41 | 62 | (defn ^:private contexts-for [root-filename query config] |
42 | 63 | (let [all-files (fs/glob root-filename (str "**" (or query "") "**")) |
|
0 commit comments