Skip to content

Commit c38589f

Browse files
committed
Store prompts as comments
Issue: #15
1 parent c395126 commit c38589f

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

src/ai-cli-config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ emacs = \C-xa
3232

3333
; Multishot command-specific prompts
3434
[prompt-gdb]
35+
comment = #
3536
user-1 = Disable breakpoint number 4
3637
assistant-1 = delete 4
3738
user-2 = break on line 67 of foo.cpp when flag is false,
3839
assistant-3 = break foo.cpp:67 if !flag
3940

4041
[prompt-bash]
42+
comment = #
4143
user-1 = List files in current directory
4244
assistant-1 = ls
4345
user-2 = How many JavaScript files in the current directory contain the word bar?
@@ -46,6 +48,7 @@ user-3 = xyzzy
4648
assistant-3 = # Sorry I can't help.
4749

4850
[prompt-sqlite3]
51+
comment = --
4952
user-1 = Show available tables
5053
assistant-1 = .tables
5154
user-2 = Show average of grade in table students
@@ -54,9 +57,13 @@ user-3 = xyzzy
5457
assistant-3 = -- Sorry I can't help.
5558

5659
[prompt-bc]
60+
comment = #
5761
user-1 = Calculate 2 raised to the 64th power
5862
assistant-1 = 2^64
5963
user-2 = Increment variable i by 1
6064
assistant-2 = i++
6165
user-3 = xyzzy
6266
assistant-3 = /* Sorry I can't help. */
67+
68+
[prompt-rl_driver]
69+
comment = #

src/ai_cli.5

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH AI_READLINE 5 "2023-12-12" "Diomidis Spinellis" \" -*-
1+
.TH AI_READLINE 5 "2024-03-17" "Diomidis Spinellis" \" -*-
22
\" nroff -*
33

44
.SH NAME
@@ -238,6 +238,14 @@ The multishot example prompts are provided by
238238
.B ai_cli
239239
at the beginning of each API request.
240240

241+
.PP
242+
\fIcomment=\fR
243+
.RS 4
244+
The line comment sequence for the corresponding tool.
245+
If this is defined, then prompt lines are stored in the
246+
history and provided as context in the form of line comments.
247+
.RE
248+
241249
.PP
242250
\fIuser-n=\fR
243251
.RS 4

src/ai_cli.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <readline/history.h>
2929

3030
#include "config.h"
31+
#include "support.h"
3132

3233
#include "fetch_anthropic.h"
3334
#include "fetch_hal.h"
@@ -52,6 +53,28 @@ static config_t config;
5253

5354
char * (*fetch)(config_t *config, const char *prompt, int history_length);
5455

56+
/*
57+
* Add the specified prompt to the RL history, as a comment if the
58+
* comment prefix is defined.
59+
*/
60+
static void
61+
add_prompt_to_history(const char *prompt)
62+
{
63+
if (prompt == NULL)
64+
return;
65+
66+
if (!config.prompt_comment_set) {
67+
add_history(prompt);
68+
return;
69+
}
70+
71+
char *commented_prompt;
72+
safe_asprintf(&commented_prompt, "%s %s", config.prompt_comment,
73+
prompt);
74+
add_history(commented_prompt);
75+
free(commented_prompt);
76+
}
77+
5578
/*
5679
* The user has has asked for AI to be queried on the typed text
5780
* Replace the user's text with the queried on
@@ -66,23 +89,30 @@ query_ai(int count, int key)
6689
prev_response = NULL;
6790
}
6891

69-
add_history(*rl_line_buffer_ptr);
92+
add_prompt_to_history(*rl_line_buffer_ptr);
7093
char *response = fetch(&config, *rl_line_buffer_ptr,
7194
*history_length_ptr);
7295
if (!response)
7396
return -1;
7497
rl_crlf();
7598
rl_on_new_line();
76-
rl_begin_undo_group();
7799
rl_delete_text(0, *rl_end_ptr);
78100
*rl_point_ptr = 0;
79101
if (config.general_response_prefix_set) {
80102
rl_insert_text(config.general_response_prefix);
81103
rl_insert_text(" ");
82104
}
83105
rl_insert_text(response);
84-
rl_end_undo_group();
85106
prev_response = response;
107+
/*
108+
* The readline_internal_teardown() function will restore
109+
* the original history line iff the line being edited
110+
* was originally in the history, AND the line has changed.
111+
* Avoid this by clearing the undo list.
112+
* This results in the commented prompt rather than the
113+
* ucommented prompt being stored in the history.
114+
*/
115+
rl_free_undo_list();
86116
return 0;
87117
}
88118

src/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ fixed_program_matcher(config_t *pconfig, const char* name, const char* value)
208208
return 1; \
209209
} \
210210
} while (0)
211-
MATCH_PROGRAM(system, safe_strdup);
211+
MATCH_PROGRAM(comment, safe_strdup);
212212
MATCH_PROGRAM(context, strtocard);
213+
MATCH_PROGRAM(system, safe_strdup);
213214

214215
return 0;
215216
}

src/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ typedef struct {
8282
int prompt_context; // # past prompts to provide as context
8383
const char *prompt_system; // System prompt
8484

85+
// Specific program's comment character to be prefixed in prompts
86+
const char *prompt_comment;
8587
// Up to three training shots for a specific program
8688
const char *prompt_user[NPROMPTS];
8789
const char *prompt_assistant[NPROMPTS];
@@ -129,6 +131,7 @@ typedef struct {
129131
bool openai_model_set;
130132
bool openai_temperature_set;
131133

134+
bool prompt_comment_set;
132135
bool prompt_context_set;
133136
bool prompt_system_set;
134137
} config_t;

src/rl_driver.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ main(int argc, char *argv[])
3030
{
3131
char *s;
3232

33+
using_history();
3334
while ((s = readline("> ")) != NULL) {
34-
printf("Read [%s]\n", s);
35+
printf("\nRead [%s]\n", s);
3536
if (*s)
3637
add_history(s);
38+
3739
free(s);
3840
}
3941
}

src/support.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ range_strdup(const char *begin, const char *end)
101101
return p;
102102
}
103103

104+
// Store the formatted output in the dynamically allocated buffer strp
104105
int
105106
safe_asprintf(char **strp, const char *fmt, ...)
106107
{

0 commit comments

Comments
 (0)