Skip to content

Commit 167703b

Browse files
authored
Chore: Refactor error investigation prompts to improve context handling (#34)
* Refactor error investigation prompts to improve context handling * bump version
1 parent f12cf54 commit 167703b

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

HISTORY.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
** Main branch change
55

6+
- Chore: Refactor error investigation prompts to improve context handling
67
- Chore: Add clipboard context option to ai-code-send-command function
78
- Chore: add compilation buffer content as error context
89
- Chore: Refactor AI shell command prompt to include marked files in dired mode

ai-code-discussion.el

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,24 @@ Argument ARG is the prefix argument."
130130
(buffer-substring-no-properties (point-min) (point-max)))))
131131
(region-text (when (region-active-p)
132132
(buffer-substring-no-properties (region-beginning) (region-end))))
133-
(context-text (or compilation-content region-text))
133+
(buffer-file buffer-file-name)
134+
(full-buffer-context (when (and (not buffer-file) (not region-text))
135+
(buffer-substring-no-properties (point-min) (point-max))))
134136
(function-name (which-function))
135-
(files-context-string (ai-code--get-context-files-string)))
137+
(files-context-string (ai-code--get-context-files-string))
138+
(context-section
139+
(if full-buffer-context
140+
(concat "\n\nContext:\n" full-buffer-context)
141+
(concat
142+
(when compilation-content
143+
(concat "\n\nCompilation output:\n" compilation-content))
144+
(when (and region-text (not compilation-content))
145+
(concat "\n\nSelected code:\n" region-text)))))
146+
(default-question "How to fix the error in this code? Please analyze the error, explain the root cause, and provide the corrected code to resolve the issue: "))
136147
(if (or arg (not (derived-mode-p 'prog-mode)))
137-
(let* ((initial-prompt (if (and context-text (not (derived-mode-p 'prog-mode)))
138-
(concat "Investigate the exception and fix the code:\n\n" context-text)
139-
(or context-text "")))
140-
(prompt (ai-code-read-string "Investigate exception (no context): " initial-prompt))
148+
(let* ((prompt (ai-code-read-string "Investigate exception (no context): " default-question))
141149
(final-prompt (concat prompt
150+
context-section
142151
(when function-name (format "\nFunction: %s" function-name))
143152
files-context-string)))
144153
(ai-code--insert-prompt final-prompt))
@@ -151,15 +160,12 @@ Argument ARG is the prefix argument."
151160
(format "Investigate exception in function %s: " function-name)
152161
"Investigate selected exception: "))
153162
(function-name
154-
(format "Investigate exceptions in function %s: " function-name))
163+
(format "Investigate exceptions in function %s: " function-name))
155164
(t "Investigate exceptions in code: ")))
156-
(initial-prompt (ai-code-read-string prompt-label
157-
(or context-text "How to fix the error in this code? Please analyze the error, explain the root cause, and provide the corrected code to resolve the issue: ")))
165+
(initial-prompt (ai-code-read-string prompt-label default-question))
158166
(final-prompt
159167
(concat initial-prompt
160-
(when compilation-content (concat "\n\nCompilation output:\n" compilation-content))
161-
(when (and region-text (not compilation-content))
162-
(concat "\n\nSelected code:\n" region-text))
168+
context-section
163169
(when function-name (format "\nFunction: %s" function-name))
164170
files-context-string
165171
(concat "\n\nNote: Please focus on how to fix the error. Your response should include:\n"

ai-code-interface.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;;; ai-code-interface.el --- AI code interface for editing AI prompt files -*- lexical-binding: t; -*-
22

33
;; Author: Kang Tu <tninja@gmail.com>
4-
;; Version: 0.48
4+
;; Version: 0.49
55
;; Package-Requires: ((emacs "26.1") (transient "0.8.0") (magit "2.1.0"))
66

77
;; SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)