Skip to content

Commit 702541b

Browse files
committed
Add history feature
1 parent ca203da commit 702541b

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Diff for: Makefile

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

@@ -21,6 +27,11 @@ tidy:
2127
test:
2228
go test -v ./...
2329

30+
cp: build-linux-amd64 build-linux-arm64
31+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript
32+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_amd64
33+
cp bin/gptscript_linux_arm64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_arm64
34+
2435
GOLANGCI_LINT_VERSION ?= v1.56.1
2536
lint:
2637
if ! command -v golangci-lint &> /dev/null; then \

Diff for: go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ 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/davecgh/go-spew v1.1.1 // indirect
57+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
5658
github.com/dlclark/regexp2 v1.4.0 // indirect
5759
github.com/dsnet/compress v0.0.1 // indirect
5860
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

@@ -194,6 +197,36 @@ func (c *Context) WrappedContext() context.Context {
194197
return context.WithValue(c.Ctx, engineContext{}, c)
195198
}
196199

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

@@ -256,6 +289,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
256289
input = ""
257290
}
258291

292+
if ctx.Parent == nil {
293+
completion.Messages = putHistory(completion.Messages)
294+
}
295+
259296
if input != "" {
260297
completion.Messages = append(completion.Messages, types.CompletionMessage{
261298
Role: types.CompletionMessageRoleTypeUser,

0 commit comments

Comments
 (0)