Skip to content

Commit b4e865b

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 b4e865b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pkg/runner/runner.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package runner
22

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

@@ -115,6 +119,9 @@ func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, inp
115119
Type: EventTypeCallFinish,
116120
Content: *result.Result,
117121
})
122+
if err := recordStateMessage(result.State); err != nil {
123+
return "", err
124+
}
118125
return *result.Result, nil
119126
}
120127

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

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

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

0 commit comments

Comments
 (0)