|
15 | 15 |
|
16 | 16 | (def ^:private logger-tag "[CONTEXT]") |
17 | 17 |
|
18 | | -(defn ^:private agents-file-contexts |
| 18 | +(defn agents-file-contexts |
19 | 19 | "Search for AGENTS.md file both in workspaceRoot and global config dir." |
20 | | - [db _config] |
| 20 | + [db] |
21 | 21 | ;; TODO make it customizable by behavior |
22 | 22 | (let [agent-file "AGENTS.md" |
23 | 23 | local-agent-files (keep (fn [{:keys [uri]}] |
|
52 | 52 | :content (llm-api/refine-file-context path lines-range)} |
53 | 53 | :partial lines-range)))) |
54 | 54 |
|
55 | | -(defn raw-contexts->refined [contexts db config] |
56 | | - (concat (agents-file-contexts db config) |
57 | | - (mapcat (fn [{:keys [type path lines-range position uri]}] |
58 | | - (case (name type) |
59 | | - "file" [(file->refined-context path lines-range)] |
60 | | - "directory" (->> (fs/glob path "**") |
61 | | - (remove fs/directory?) |
62 | | - (map (fn [path] |
63 | | - (let [filename (str (fs/canonicalize path))] |
64 | | - (file->refined-context filename nil))))) |
65 | | - "repoMap" [{:type :repoMap}] |
66 | | - "cursor" [{:type :cursor |
67 | | - :path path |
68 | | - :position position}] |
69 | | - "mcpResource" (try |
70 | | - (mapv |
71 | | - (fn [{:keys [text]}] |
72 | | - {:type :mcpResource |
73 | | - :uri uri |
74 | | - :content text}) |
75 | | - (:contents (f.mcp/get-resource! uri db))) |
76 | | - (catch Exception e |
77 | | - (logger/warn logger-tag (format "Error getting MCP resource %s: %s" uri (.getMessage e))) |
78 | | - [])) |
79 | | - nil)) |
80 | | - contexts))) |
| 55 | +(defn raw-contexts->refined [contexts db] |
| 56 | + (mapcat (fn [{:keys [type path lines-range position uri]}] |
| 57 | + (case (name type) |
| 58 | + "file" [(file->refined-context path lines-range)] |
| 59 | + "directory" (->> (fs/glob path "**") |
| 60 | + (remove fs/directory?) |
| 61 | + (map (fn [path] |
| 62 | + (let [filename (str (fs/canonicalize path))] |
| 63 | + (file->refined-context filename nil))))) |
| 64 | + "repoMap" [{:type :repoMap}] |
| 65 | + "cursor" [{:type :cursor |
| 66 | + :path path |
| 67 | + :position position}] |
| 68 | + "mcpResource" (try |
| 69 | + (mapv |
| 70 | + (fn [{:keys [text]}] |
| 71 | + {:type :mcpResource |
| 72 | + :uri uri |
| 73 | + :content text}) |
| 74 | + (:contents (f.mcp/get-resource! uri db))) |
| 75 | + (catch Exception e |
| 76 | + (logger/warn logger-tag (format "Error getting MCP resource %s: %s" uri (.getMessage e))) |
| 77 | + [])) |
| 78 | + nil)) |
| 79 | + contexts)) |
| 80 | + |
| 81 | +(defn contexts-str-from-prompt |
| 82 | + "Extract all contexts (@something) and refine them." |
| 83 | + [prompt db] |
| 84 | + (let [context-pattern #"[@]([^\s]+)" |
| 85 | + matches (re-seq context-pattern prompt)] |
| 86 | + (when (seq matches) |
| 87 | + (let [raw-contexts (mapv (fn [[full-match path]] |
| 88 | + (let [type (if (string/starts-with? full-match "@") |
| 89 | + "file" |
| 90 | + "file")] |
| 91 | + {:type type |
| 92 | + :path path})) |
| 93 | + matches)] |
| 94 | + (raw-contexts->refined raw-contexts db))))) |
81 | 95 |
|
82 | 96 | (defn ^:private all-files-from* [root-filename] (fs/glob root-filename "**")) |
83 | 97 | (def ^:private all-files-from (memoize all-files-from*)) |
|
0 commit comments