summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gptel-tools-hl.el58
1 files changed, 27 insertions, 31 deletions
diff --git a/gptel-tools-hl.el b/gptel-tools-hl.el
index 45d44fe..fd08260 100644
--- a/gptel-tools-hl.el
+++ b/gptel-tools-hl.el
@@ -57,50 +57,46 @@ Precisely: https://raw.githubusercontent.com/can1357/oh-my-pi/85003ca/packages/c
57(defun hl-hash (line) 57(defun hl-hash (line)
58 (elt hl-bigrams (% (sxhash line) 647))) 58 (elt hl-bigrams (% (sxhash line) 647)))
59 59
60(defun hl-file-read (file) 60(defun hl-file-lines (file &optional beg end)
61 "Return FILE contents in hashline format." 61 "Return FILE contents as a list of strings.
62With optional BEG and END (inclusive, 1-based), return the lines between that range."
62 (with-temp-buffer 63 (with-temp-buffer
63 (insert-file-contents-literally file) 64 (insert-file-contents-literally file)
64 (let ((lines (string-lines (buffer-string)))) 65 (let ((lines (string-lines (buffer-string))))
65 (string-join 66 (if beg
66 (cl-mapcar (lambda (line i) 67 (cl-subseq lines (1- beg) end)
67 (format "%d%2s|%s" i (hl-hash line) line)) 68 lines))))
68 lines 69
69 (number-sequence 1 (length lines))) 70(defun hl-format-lines (lines &optional start include-content?)
70 "\n")))) 71 "Format LINES in hashline format.
72START is 1-based (default 1).
73If INCLUDE-CONTENT is non-nil, append `|LINE' after each hash."
74 (string-join
75 (cl-mapcar (lambda (line i)
76 (if include-content?
77 (format "%d%2s|%s" i (hl-hash line) line)
78 (format "%d%2s" i (hl-hash line))))
79 lines
80 (number-sequence (or start 1) (+ (or start 1) (1- (length lines)))))
81 "\n"))
82
83(defun hl-file-read (file)
84 "Return FILE contents in hashline format."
85 (hl-format-lines (hl-file-lines file) nil t))
71 86
72(defun hl-file-read-range (file beg end) 87(defun hl-file-read-range (file beg end)
73 "Return FILE contents in hashline format from lines BEG to END." 88 "Return FILE contents in hashline format from lines BEG to END."
74 (with-temp-buffer 89 (hl-format-lines (hl-file-lines file beg end) beg t))
75 (insert-file-contents-literally file)
76 (let ((lines (cl-subseq (string-lines (buffer-string))
77 (1- beg) end)))
78 (string-join
79 (cl-mapcar (lambda (line i)
80 (format "%d%2s|%s" i (hl-hash line) line))
81 lines
82 (number-sequence beg (+ beg (1- (length lines)))))
83 "\n"))))
84 90
85;; FIXME might be redundant with =hl-file-line-hash= if I'm only 91;; FIXME might be redundant with =hl-file-line-hash= if I'm only
86;; verifying the hash of the anchors and telling LLM to re-read the 92;; verifying the hash of the anchors and telling LLM to re-read the
87;; file when they don't match 93;; file when they don't match
88(defun hl-file-read-hashes (file) 94(defun hl-file-read-hashes (file)
89 "Return FILE contents in hashline format without content." 95 "Return FILE contents in hashline format without content."
90 (with-temp-buffer 96 (hl-format-lines (hl-file-lines file)))
91 (insert-file-contents-literally file)
92 (let ((lines (string-lines (buffer-string))))
93 (string-join
94 (cl-mapcar (lambda (line i)
95 (format "%d%2s" i (hl-hash line)))
96 lines
97 (number-sequence 1 (length lines)))
98 "\n"))))
99 97
100(defun hl-file-line-hash (file n) 98(defun hl-file-line-hash (file n)
101 "Return the hash of the Nth line in FILE." 99 "Return the hash of the Nth line in FILE."
102 (with-temp-buffer 100 (let ((lines (hl-file-lines file)))
103 (insert-file-contents-literally file) 101 (hl-hash (elt lines (1- n)))))
104 (let ((lines (string-lines (buffer-string))))
105 (hl-hash (elt lines (1- n))))))
106 102