Skip to content

Commit 228334d

Browse files
committed
Chore: Add ability to save state message from each gptscript call
Signed-off-by: Daishan Peng <[email protected]>
1 parent 969450b commit 228334d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

pkg/runner/runner.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package runner
22

33
import (
44
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"path/filepath"
59
"sync"
610
"time"
711

812
"github.com/gptscript-ai/gptscript/pkg/engine"
913
"github.com/gptscript-ai/gptscript/pkg/types"
1014
"golang.org/x/sync/errgroup"
15+
"google.golang.org/appengine/log"
1116
)
1217

1318
type MonitorFactory interface {
@@ -115,6 +120,9 @@ func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, inp
115120
Type: EventTypeCallFinish,
116121
Content: *result.Result,
117122
})
123+
if err := recordStateMessage(result.State); err != nil {
124+
log.Warningf(callCtx.Ctx, "Failed to record state message: %v", err)
125+
}
118126
return *result.Result, nil
119127
}
120128

@@ -218,3 +226,24 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
218226

219227
return
220228
}
229+
230+
// recordStateMessage record the final state of the openai request and fetch messages and tools for analysis purpose
231+
// The name follows `gptscript-state-${hostname}-${unixtimestamp}`
232+
func recordStateMessage(state *engine.State) error {
233+
if state == nil {
234+
return nil
235+
}
236+
tmpdir := os.TempDir()
237+
data, err := json.Marshal(state)
238+
if err != nil {
239+
return err
240+
}
241+
242+
hostname, err := os.Hostname()
243+
if err != nil {
244+
return err
245+
}
246+
247+
filename := filepath.Join(tmpdir, fmt.Sprintf("gptscript-state-%v-%v", hostname, time.Now().Unix()))
248+
return os.WriteFile(filename, data, 0444)
249+
}

scripts/upload-s3.gpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Tools: sys.exec, upload-s3
2+
3+
Find all files starts with `gptscript-state` in $TMPDIR, and upload the file to s3 bucket `gptscript-state`. Only find file from top-level folder.
4+
Use filename to identify the file that is only uploaded within last week.
5+
6+
---
7+
name: upload-s3
8+
description: upload all the file to s3 bucket
9+
args: input: args to provide for aws s3 command
10+
11+
aws s3 ${input}
12+

scripts/upload-s3.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
find $TMPDIR -maxdepth 1 -type f -name 'gptscript-state*' -mtime -7 | xargs -I{} aws s3 cp {} s3://gptscript-state/

0 commit comments

Comments
 (0)