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
@@ -256,6 +259,39 @@ func (c *Context) WrappedContext() context.Context {
256
259
return context .WithValue (c .Ctx , engineContext {}, c )
257
260
}
258
261
262
+ func putHistory (messages []types.CompletionMessage ) []types.CompletionMessage {
263
+ prevHistoryFile := strings .TrimSpace (os .Getenv ("GPTSCRIPT_PREVIOUS_HISTORY_FILE" ))
264
+
265
+ if prevHistoryFile == "" {
266
+ return messages
267
+ }
268
+ fp , err := os .Open (prevHistoryFile )
269
+ if err != nil {
270
+ slog .Error ("Open Error" , err )
271
+ return messages
272
+ }
273
+ defer fp .Close ()
274
+
275
+ scanner := bufio .NewScanner (fp )
276
+
277
+ prevMessages := []types.CompletionMessage {}
278
+ for scanner .Scan () {
279
+ var message types.CompletionMessage
280
+ line := scanner .Text ()
281
+ err := json .Unmarshal ([]byte (line ), & message )
282
+ if err != nil {
283
+ slog .Error ("Unmarshal Error" , err )
284
+ return messages
285
+ }
286
+ if message .Role == "system" {
287
+ continue
288
+ }
289
+ prevMessages = append (prevMessages , message )
290
+ }
291
+
292
+ return append (messages , prevMessages ... )
293
+ }
294
+
259
295
func (e * Engine ) Start (ctx Context , input string ) (ret * Return , _ error ) {
260
296
tool := ctx .Tool
261
297
@@ -274,6 +310,8 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
274
310
return e .runOpenAPI (tool , input )
275
311
} else if tool .IsEcho () {
276
312
return e .runEcho (tool )
313
+ } else if tool .IsBreak () {
314
+ return e .runBreak (tool , input )
277
315
}
278
316
s , err := e .runCommand (ctx , tool , input , ctx .ToolCategory )
279
317
if err != nil {
@@ -314,6 +352,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
314
352
input = ""
315
353
}
316
354
355
+ if ctx .Parent == nil {
356
+ completion .Messages = putHistory (completion .Messages )
357
+ }
358
+
317
359
if input != "" {
318
360
completion .Messages = append (completion .Messages , types.CompletionMessage {
319
361
Role : types .CompletionMessageRoleTypeUser ,
0 commit comments