Skip to content

Commit 176f554

Browse files
committed
Add history feature
1 parent 622ce1e commit 176f554

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Diff for: Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ build-ui:
1212
build-exe:
1313
GOOS=windows go build -o bin/gptscript.exe -tags "${GO_TAGS}" .
1414

15+
build-linux-amd64:
16+
GOOS=linux GOARCH=amd64 go build -o bin/gptscript_linux_amd64 -tags "${GO_TAGS}" .
17+
18+
build-linux-arm64:
19+
GOOS=linux GOARCH=arm64 go build -o bin/gptscript_linux_arm64 -tags "${GO_TAGS}" .
20+
1521
build:
1622
CGO_ENABLED=0 go build -o bin/gptscript -tags "${GO_TAGS}" -ldflags "-s -w" .
1723

@@ -22,6 +28,12 @@ test:
2228
go test -v ./...
2329

2430
GOLANGCI_LINT_VERSION ?= v1.59.0
31+
32+
cp: build-linux-amd64 build-linux-arm64
33+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript
34+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_amd64
35+
cp bin/gptscript_linux_arm64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_arm64
36+
2537
lint:
2638
if ! command -v golangci-lint &> /dev/null; then \
2739
echo "Could not find golangci-lint, installing version $(GOLANGCI_LINT_VERSION)."; \
@@ -59,4 +71,4 @@ validate-docs:
5971
echo "Encountered dirty repo!"; \
6072
git diff; \
6173
exit 1 \
62-
;fi
74+
;fi

Diff for: go.mod

+2
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

Diff for: pkg/engine/engine.go

+37
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

@@ -201,6 +204,36 @@ func (c *Context) WrappedContext() context.Context {
201204
return context.WithValue(c.Ctx, engineContext{}, c)
202205
}
203206

207+
func putHistory(messages []types.CompletionMessage) []types.CompletionMessage {
208+
prevHistoryFile := strings.TrimSpace(os.Getenv("GPTSCRIPT_PREVIOUS_HISTORY_FILE"))
209+
210+
if prevHistoryFile == "" {
211+
return messages
212+
}
213+
fp, err := os.Open(prevHistoryFile)
214+
if err != nil {
215+
slog.Error("Open Error", err)
216+
return messages
217+
}
218+
defer fp.Close()
219+
220+
scanner := bufio.NewScanner(fp)
221+
222+
prevMessages := []types.CompletionMessage{}
223+
for scanner.Scan() {
224+
var message types.CompletionMessage
225+
line := scanner.Text()
226+
err := json.Unmarshal([]byte(line), &message)
227+
if err != nil {
228+
slog.Error("Unmarshal Error", err)
229+
return messages
230+
}
231+
prevMessages = append(prevMessages, message)
232+
}
233+
234+
return append(messages, prevMessages...)
235+
}
236+
204237
func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
205238
tool := ctx.Tool
206239

@@ -263,6 +296,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
263296
input = ""
264297
}
265298

299+
if ctx.Parent == nil {
300+
completion.Messages = putHistory(completion.Messages)
301+
}
302+
266303
if input != "" {
267304
completion.Messages = append(completion.Messages, types.CompletionMessage{
268305
Role: types.CompletionMessageRoleTypeUser,

0 commit comments

Comments
 (0)