Skip to content

Commit f26b36c

Browse files
committed
Add allganize feature: history, break, GPTSCRIPT_PROGRESS_TIME_STEP_MS
1 parent 8f7d28f commit f26b36c

File tree

7 files changed

+105
-7
lines changed

7 files changed

+105
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
/binaries
1010
/checksums.txt
1111
/.env*
12+
ui/
13+
gptscript.code-workspace

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

@@ -23,6 +29,12 @@ smoke:
2329
go test -v -tags='smoke' ./pkg/tests/smoke/...
2430

2531
GOLANGCI_LINT_VERSION ?= v1.59.0
32+
33+
cp: build-linux-amd64 build-linux-arm64
34+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript
35+
cp bin/gptscript_linux_amd64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_amd64
36+
cp bin/gptscript_linux_arm64 ~/Workspace/streamlit-gptscript/gptscript/bin/gptscript_linux_arm64
37+
2638
lint:
2739
if ! command -v golangci-lint &> /dev/null; then \
2840
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
@@ -48,13 +48,15 @@ require (
4848
github.com/bodgit/plumbing v1.2.0 // indirect
4949
github.com/bodgit/sevenzip v1.3.0 // indirect
5050
github.com/bodgit/windows v1.0.0 // indirect
51+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5152
github.com/charmbracelet/glamour v0.7.0 // indirect
5253
github.com/charmbracelet/lipgloss v0.11.0 // indirect
5354
github.com/charmbracelet/x/ansi v0.1.1 // indirect
5455
github.com/connesc/cipherio v0.2.1 // indirect
5556
github.com/containerd/console v1.0.4 // indirect
5657
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
5758
github.com/davecgh/go-spew v1.1.1 // indirect
59+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
5860
github.com/dlclark/regexp2 v1.4.0 // indirect
5961
github.com/dsnet/compress v0.0.1 // indirect
6062
github.com/go-openapi/jsonpointer v0.20.2 // indirect

pkg/engine/control.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package engine
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/gptscript-ai/gptscript/pkg/types"
8+
)
9+
10+
func (e *Engine) runBreak(tool types.Tool, input string) (cmdOut *Return, cmdErr error) {
11+
info, err := json.Marshal(tool)
12+
if err != nil {
13+
return nil, err
14+
}
15+
var dict map[string]interface{}
16+
json.Unmarshal(info, &dict)
17+
dict["input"] = input
18+
info, err = json.Marshal(dict)
19+
return nil, fmt.Errorf("TOOL_BREAK: %s", info)
20+
}

pkg/engine/engine.go

Lines changed: 42 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

@@ -256,6 +259,39 @@ func (c *Context) WrappedContext() context.Context {
256259
return context.WithValue(c.Ctx, engineContext{}, c)
257260
}
258261

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+
259295
func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
260296
tool := ctx.Tool
261297

@@ -274,6 +310,8 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
274310
return e.runOpenAPI(tool, input)
275311
} else if tool.IsEcho() {
276312
return e.runEcho(tool)
313+
} else if tool.IsBreak() {
314+
return e.runBreak(tool, input)
277315
}
278316
s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory)
279317
if err != nil {
@@ -314,6 +352,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
314352
input = ""
315353
}
316354

355+
if ctx.Parent == nil {
356+
completion.Messages = putHistory(completion.Messages)
357+
}
358+
317359
if input != "" {
318360
completion.Messages = append(completion.Messages, types.CompletionMessage{
319361
Role: types.CompletionMessageRoleTypeUser,

pkg/runner/runner.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"os"
89
"sort"
10+
"strconv"
911
"strings"
1012
"sync"
1113
"time"
@@ -672,17 +674,29 @@ func streamProgress(callCtx *engine.Context, monitor Monitor) (chan<- types.Comp
672674

673675
wg := sync.WaitGroup{}
674676
wg.Add(1)
677+
progressTimeStepMs, err := strconv.Atoi(os.Getenv("GPTSCRIPT_PROGRESS_TIME_STEP_MS"))
678+
if err != nil {
679+
// 기본값 250ms를 사용하거나 오류를 처리합니다.
680+
progressTimeStepMs = 250
681+
}
682+
progressTimeStep := time.Duration(progressTimeStepMs) * time.Millisecond
675683
go func() {
676684
defer wg.Done()
685+
lastSentTimeMap := make(map[string]time.Time)
677686
for status := range progress {
678687
if message := status.PartialResponse; message != nil {
679-
monitor.Event(Event{
680-
Time: time.Now(),
681-
CallContext: callCtx.GetCallContext(),
682-
Type: EventTypeCallProgress,
683-
ChatCompletionID: status.CompletionID,
684-
Content: getEventContent(message.String(), *callCtx),
685-
})
688+
now := time.Now()
689+
lastSentTime, ok := lastSentTimeMap[status.CompletionID]
690+
if !ok || now.Sub(lastSentTime) > progressTimeStep {
691+
lastSentTimeMap[status.CompletionID] = now
692+
monitor.Event(Event{
693+
Time: time.Now(),
694+
CallContext: callCtx.GetCallContext(),
695+
Type: EventTypeCallProgress,
696+
ChatCompletionID: status.CompletionID,
697+
Content: getEventContent(message.String(), *callCtx),
698+
})
699+
}
686700
} else {
687701
monitor.Event(Event{
688702
Time: time.Now(),
@@ -694,6 +708,7 @@ func streamProgress(callCtx *engine.Context, monitor Monitor) (chan<- types.Comp
694708
Usage: status.Usage,
695709
ChatResponseCached: status.Cached,
696710
})
711+
delete(lastSentTimeMap, status.CompletionID)
697712
}
698713
}
699714
}()

pkg/types/tool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
DaemonPrefix = "#!sys.daemon"
2020
OpenAPIPrefix = "#!sys.openapi"
2121
EchoPrefix = "#!sys.echo"
22+
BreakPrefix = "#!sys.break"
2223
CommandPrefix = "#!"
2324
)
2425

@@ -771,6 +772,10 @@ func (t Tool) IsEcho() bool {
771772
return strings.HasPrefix(t.Instructions, EchoPrefix)
772773
}
773774

775+
func (t Tool) IsBreak() bool {
776+
return strings.HasPrefix(t.Instructions, BreakPrefix)
777+
}
778+
774779
func (t Tool) IsHTTP() bool {
775780
return strings.HasPrefix(t.Instructions, "#!http://") ||
776781
strings.HasPrefix(t.Instructions, "#!https://")

0 commit comments

Comments
 (0)