1
1
package engine
2
2
3
3
import (
4
+ "bufio"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
8
+ "log/slog"
9
+ "os"
7
10
"strings"
8
11
"sync"
9
12
@@ -281,6 +284,39 @@ func populateMessageParams(ctx Context, completion *types.CompletionRequest, too
281
284
return nil
282
285
}
283
286
287
+ func putHistory (messages []types.CompletionMessage ) []types.CompletionMessage {
288
+ prevHistoryFile := strings .TrimSpace (os .Getenv ("GPTSCRIPT_PREVIOUS_HISTORY_FILE" ))
289
+
290
+ if prevHistoryFile == "" {
291
+ return messages
292
+ }
293
+ fp , err := os .Open (prevHistoryFile )
294
+ if err != nil {
295
+ slog .Error ("Open Error" , err )
296
+ return messages
297
+ }
298
+ defer fp .Close ()
299
+
300
+ scanner := bufio .NewScanner (fp )
301
+
302
+ prevMessages := []types.CompletionMessage {}
303
+ for scanner .Scan () {
304
+ var message types.CompletionMessage
305
+ line := scanner .Text ()
306
+ err := json .Unmarshal ([]byte (line ), & message )
307
+ if err != nil {
308
+ slog .Error ("Unmarshal Error" , err )
309
+ return messages
310
+ }
311
+ if message .Role == "system" {
312
+ continue
313
+ }
314
+ prevMessages = append (prevMessages , message )
315
+ }
316
+
317
+ return append (messages , prevMessages ... )
318
+ }
319
+
284
320
func (e * Engine ) Start (ctx Context , input string ) (ret * Return , _ error ) {
285
321
tool := ctx .Tool
286
322
@@ -299,6 +335,8 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
299
335
return e .runOpenAPI (tool , input )
300
336
} else if tool .IsEcho () {
301
337
return e .runEcho (tool )
338
+ } else if tool .IsBreak () {
339
+ return e .runBreak (tool , input )
302
340
}
303
341
s , err := e .runCommand (ctx , tool , input , ctx .ToolCategory )
304
342
if err != nil {
@@ -322,6 +360,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
322
360
input = ""
323
361
}
324
362
363
+ if ctx .Parent == nil {
364
+ completion .Messages = putHistory (completion .Messages )
365
+ }
366
+
325
367
if input != "" {
326
368
completion .Messages = append (completion .Messages , types.CompletionMessage {
327
369
Role : types .CompletionMessageRoleTypeUser ,
0 commit comments