Skip to content

Commit ec565e4

Browse files
author
Kang Tu
committed
Add y-or-n-p opt-in before inserting prompt into buffer
1 parent 85a51da commit ec565e4

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎ai-code-doc.el‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@
3030
("Derive DDD Context for Repo" . ai-code-derive-ddd-context)
3131
("Derive Test Context Document" . ai-code-derive-test-context)
3232
("Derive Unit Test to Help Understand the Topic" . ai-code-derive-topic-unit-tests))
33-
;; DONE: add an option: Derive Unit Test to Help Understand the topic. Given the current repo and a user entered topic, generate unit-tests to help the user understand code related to the topic. These tests should be written in one or more unit-test class. Tests / Class should be organized in a way to help user understand the code related to the topic gradually, from very basic to advance, from common use case to edge case. Make sure the test code is easy to understand and well commented. The tests should be runnable. The purpose is to let user understand the code related to the topic by reading and running the tests.
3433
"Choices for `ai-code-derive-architecture-document'.")
3534

3635
(defun ai-code--doc-emit-prompt (title prompt)
3736
"Insert PROMPT under a TITLE headline at point, or send it to the AI.
38-
In `ai-code-prompt-mode' the prompt is written into the current buffer
39-
under the cursor, as an Org section at the level of the surrounding
40-
section, and no AI request is made. Everywhere else PROMPT is handed to
41-
`ai-code--insert-prompt' as usual."
42-
(if (derived-mode-p 'ai-code-prompt-mode)
37+
In `ai-code-prompt-mode' the user is asked whether to write the prompt
38+
into the current buffer, as an Org section at the level of the
39+
surrounding section, in which case no AI request is made. When the
40+
answer is no, and everywhere outside `ai-code-prompt-mode', PROMPT is
41+
handed to `ai-code--insert-prompt' as usual."
42+
(if (and (derived-mode-p 'ai-code-prompt-mode)
43+
(y-or-n-p "Insert prompt into this buffer instead of sending it to the AI? "))
4344
(let ((level (or (org-current-level) 1)))
4445
(unless (bolp)
4546
(insert "\n"))

‎test/test_ai-code-doc.el‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ The unit test topic question is recognized by its prompt prefix."
416416
((symbol-function 'ai-code-plain-read-string)
417417
(lambda (_prompt &optional initial-input) initial-input))
418418
((symbol-function 'ai-code--insert-prompt)
419-
(lambda (prompt) (setq sent-prompt prompt))))
419+
(lambda (prompt) (setq sent-prompt prompt)))
420+
((symbol-function 'y-or-n-p) (lambda (&rest _args) t)))
420421
(with-temp-buffer
421422
(ai-code-prompt-mode)
422423
(insert "* Existing task\n** Sub task\n")
@@ -429,6 +430,27 @@ The unit test topic question is recognized by its prompt prefix."
429430
(regexp-quote "Domain-Driven Design (DDD) style context document")
430431
(buffer-string))))))))
431432

433+
(ert-deftest ai-code-test-doc-emit-prompt-declines-buffer-dump-and-sends-to-ai ()
434+
"Declining the question in `ai-code-prompt-mode' sends the prompt to the AI.
435+
The buffer dump must stay opt-in, so a no answer falls back to the
436+
behavior used outside `ai-code-prompt-mode'."
437+
(let (sent-prompt
438+
captured-question)
439+
(cl-letf (((symbol-function 'y-or-n-p)
440+
(lambda (question)
441+
(setq captured-question question)
442+
nil))
443+
((symbol-function 'ai-code--insert-prompt)
444+
(lambda (prompt) (setq sent-prompt prompt))))
445+
(with-temp-buffer
446+
(ai-code-prompt-mode)
447+
(insert "* Existing task\n")
448+
(goto-char (point-max))
449+
(ai-code--doc-emit-prompt "Derive Repository Map" "PROMPT BODY")
450+
(should captured-question)
451+
(should (equal sent-prompt "PROMPT BODY"))
452+
(should (equal (buffer-string) "* Existing task\n"))))))
453+
432454
(ert-deftest ai-code-test-derive-architecture-guardrails-inserts-into-prompt-mode-buffer ()
433455
"In `ai-code-prompt-mode' the guardrails prompt is written at point, not sent."
434456
(ai-code-file-with-test-env
@@ -440,7 +462,8 @@ The unit test topic question is recognized by its prompt prefix."
440462
((symbol-function 'ai-code-plain-read-string)
441463
(lambda (_prompt initial-input) initial-input))
442464
((symbol-function 'ai-code--insert-prompt)
443-
(lambda (prompt) (setq sent-prompt prompt))))
465+
(lambda (prompt) (setq sent-prompt prompt)))
466+
((symbol-function 'y-or-n-p) (lambda (&rest _args) t)))
444467
(with-temp-buffer
445468
(ai-code-prompt-mode)
446469
(insert "* Existing task\n")

0 commit comments

Comments
 (0)