Skip to content

Commit 5ccfe66

Browse files
committed
Add history feature
1 parent c25c7be commit 5ccfe66

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ all: build
55
build-exe:
66
GOOS=windows go build -o bin/gptscript.exe -tags "${GO_TAGS}" .
77

8+
build-linux-amd64:
9+
GOOS=linux GOARCH=amd64 go build -o bin/gptscript_linux_amd64 -tags "${GO_TAGS}" .
10+
11+
build-linux-arm64:
12+
GOOS=linux GOARCH=arm64 go build -o bin/gptscript_linux_arm64 -tags "${GO_TAGS}" .
13+
814
build:
915
CGO_ENABLED=0 go build -o bin/gptscript -tags "${GO_TAGS}" -ldflags "-s -w" .
1016

@@ -19,6 +25,12 @@ smoke:
1925
go test -v -tags='smoke' ./pkg/tests/smoke/...
2026

2127
GOLANGCI_LINT_VERSION ?= v1.59.0
28+
29+
cp: build-linux-amd64 build-linux-arm64
30+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript
31+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_amd64
32+
cp bin/gptscript_linux_arm64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_arm64
33+
2234
lint:
2335
if ! command -v golangci-lint &> /dev/null; then \
2436
echo "Could not find golangci-lint, installing version $(GOLANGCI_LINT_VERSION)."; \

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ require (
4747
github.com/bodgit/plumbing v1.2.0 // indirect
4848
github.com/bodgit/sevenzip v1.3.0 // indirect
4949
github.com/bodgit/windows v1.0.0 // indirect
50+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5051
github.com/charmbracelet/glamour v0.7.0 // indirect
5152
github.com/charmbracelet/lipgloss v0.11.0 // indirect
5253
github.com/charmbracelet/x/ansi v0.1.1 // indirect
5354
github.com/connesc/cipherio v0.2.1 // indirect
5455
github.com/containerd/console v1.0.4 // indirect
5556
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
5657
github.com/davecgh/go-spew v1.1.1 // indirect
58+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
5759
github.com/dlclark/regexp2 v1.4.0 // indirect
5860
github.com/dsnet/compress v0.0.1 // indirect
5961
github.com/go-openapi/jsonpointer v0.20.2 // indirect

pkg/engine/engine.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package engine
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"fmt"
8+
"log/slog"
9+
"os"
710
"strings"
811
"sync"
912

@@ -228,6 +231,36 @@ func (c *Context) WrappedContext() context.Context {
228231
return context.WithValue(c.Ctx, engineContext{}, c)
229232
}
230233

234+
func putHistory(messages []types.CompletionMessage) []types.CompletionMessage {
235+
prevHistoryFile := strings.TrimSpace(os.Getenv("GPTSCRIPT_PREVIOUS_HISTORY_FILE"))
236+
237+
if prevHistoryFile == "" {
238+
return messages
239+
}
240+
fp, err := os.Open(prevHistoryFile)
241+
if err != nil {
242+
slog.Error("Open Error", err)
243+
return messages
244+
}
245+
defer fp.Close()
246+
247+
scanner := bufio.NewScanner(fp)
248+
249+
prevMessages := []types.CompletionMessage{}
250+
for scanner.Scan() {
251+
var message types.CompletionMessage
252+
line := scanner.Text()
253+
err := json.Unmarshal([]byte(line), &message)
254+
if err != nil {
255+
slog.Error("Unmarshal Error", err)
256+
return messages
257+
}
258+
prevMessages = append(prevMessages, message)
259+
}
260+
261+
return append(messages, prevMessages...)
262+
}
263+
231264
func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
232265
tool := ctx.Tool
233266

@@ -290,6 +323,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
290323
input = ""
291324
}
292325

326+
if ctx.Parent == nil {
327+
completion.Messages = putHistory(completion.Messages)
328+
}
329+
293330
if input != "" {
294331
completion.Messages = append(completion.Messages, types.CompletionMessage{
295332
Role: types.CompletionMessageRoleTypeUser,

0 commit comments

Comments
 (0)