@@ -2,12 +2,17 @@ package runner
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "fmt"
7
+ "os"
8
+ "path/filepath"
5
9
"sync"
6
10
"time"
7
11
8
12
"github.com/gptscript-ai/gptscript/pkg/engine"
9
13
"github.com/gptscript-ai/gptscript/pkg/types"
10
14
"golang.org/x/sync/errgroup"
15
+ "google.golang.org/appengine/log"
11
16
)
12
17
13
18
type MonitorFactory interface {
@@ -115,6 +120,9 @@ func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, inp
115
120
Type : EventTypeCallFinish ,
116
121
Content : * result .Result ,
117
122
})
123
+ if err := recordStateMessage (result .State ); err != nil {
124
+ log .Warningf (callCtx .Ctx , "Failed to record state message: %v" , err )
125
+ }
118
126
return * result .Result , nil
119
127
}
120
128
@@ -218,3 +226,24 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
218
226
219
227
return
220
228
}
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
+ }
0 commit comments