Skip to content

Commit eaab3c9

Browse files
committed
Add support for file contexts with line ranges
1 parent 1b11612 commit eaab3c9

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add support for file contexts with line ranges.
6+
57
## 0.10.3
68

79
- Fix openai `max_output_tokens` message.

docs/protocol.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ interface FileContext {
289289
* Path to the file
290290
*/
291291
path: string;
292+
293+
/**
294+
* Range of lines to retrive from file, if nil consider whole file.
295+
*/
296+
linesRange?: {
297+
start: number;
298+
end: number;
299+
}
292300
}
293301

294302
/**

src/eca/features/chat.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
(def ^:private logger-tag "[CHAT]")
1919

2020
(defn ^:private raw-contexts->refined [contexts]
21-
(mapcat (fn [{:keys [type path]}]
21+
(mapcat (fn [{:keys [type path lines-range]}]
2222
(case type
2323
"file" [{:type :file
2424
:path path
25-
:content (llm-api/refine-file-context path)}]
25+
:partial (boolean lines-range)
26+
:content (llm-api/refine-file-context path lines-range)}]
2627
"directory" (->> (fs/glob path "**")
2728
(remove fs/directory?)
2829
(map (fn [path]
2930
(let [filename (str (fs/canonicalize path))]
3031
{:type :file
3132
:path filename
32-
:content (llm-api/refine-file-context filename)}))))
33+
:content (llm-api/refine-file-context filename nil)}))))
3334
"repoMap" [{:type :repoMap}]))
3435
contexts))
3536

src/eca/features/prompt.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
""
3232
"<contexts>"
3333
(reduce
34-
(fn [context-str {:keys [type path content]}]
34+
(fn [context-str {:keys [type path content partial]}]
3535
(str context-str (case type
36-
:file (format "<file path=\"%s\">%s</file>\n" path content)
36+
:file (if partial
37+
(format "<file partial=true path=\"%s\">...\n%s\n...</file>\n" path content)
38+
(format "<file path=\"%s\">%s</file>\n" path content))
3739
:repoMap (format "<repoMap description=\"Workspaces structure in a tree view, spaces represent file hierarchy\" >%s</repoMap>" @repo-map*)
3840
"")))
3941
""

src/eca/llm_api.clj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
(llm-providers.ollama/list-models {:host (:host (:ollama config))
1616
:port (:port (:ollama config))}))
1717

18-
(defn refine-file-context [path]
19-
;; TODO ask LLM for the most relevant parts of the path
20-
(slurp path))
18+
;; TODO ask LLM for the most relevant parts of the path
19+
(defn refine-file-context [path lines-range]
20+
(let [content (slurp path)]
21+
(if lines-range
22+
(let [lines (string/split-lines content)
23+
start (dec (:start lines-range))
24+
end (min (count lines) (:end lines-range))]
25+
(string/join "\n" (subvec lines start end)))
26+
content)))
2127

2228
(defn ^:private anthropic-api-key [config]
2329
(or (:anthropicApiKey config)

test/eca/features/prompt_test.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
(ns eca.features.prompt-test
22
(:require
3+
[clojure.string :as string]
34
[clojure.test :refer [deftest is testing]]
4-
[eca.features.prompt :as prompt]
5-
[clojure.string :as string]))
5+
[eca.features.prompt :as prompt]))
66

77
(deftest build-instructions-test
88
(testing "Should create instructions with rules, contexts, and behavior"
99
(let [refined-contexts [{:type :file :path "foo.clj" :content "(ns foo)"}
10+
{:type :file :path "bar.clj" :content "(def a 1)" :partial true}
1011
{:type :repoMap :path nil :content nil}]
1112
rules [{:name "rule1" :content "First rule"}
1213
{:name "rule2" :content "Second rule"}]
@@ -19,6 +20,7 @@
1920
(is (string/includes? result "<rule name=\"rule2\">Second rule</rule>"))
2021
(is (string/includes? result "<contexts>"))
2122
(is (string/includes? result "<file path=\"foo.clj\">(ns foo)</file>"))
23+
(is (string/includes? result "<file partial=true path=\"bar.clj\">...\n(def a 1)\n...</file>"))
2224
(is (string/includes? result "<repoMap description=\"Workspaces structure in a tree view, spaces represent file hierarchy\" >TREE</repoMap>"))
2325
(is (string/includes? result "</contexts>"))
2426
(is (string? result)))))

0 commit comments

Comments
 (0)