@@ -65,12 +65,13 @@ type GPTScript struct {
6565 Ports string `usage:"The port range to use for ephemeral daemon ports (ex: 11000-12000)" hidden:"true"`
6666 CredentialContext string `usage:"Context name in which to store credentials" default:"default"`
6767 CredentialOverride string `usage:"Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234)"`
68- ChatState string `usage:"The chat state to continue, or null to start a new chat and return the state"`
69- ForceChat bool `usage:"Force an interactive chat session if even the top level tool is not a chat tool"`
70- ForceSequential bool `usage:"Force parallel calls to run sequentially"`
68+ ChatState string `usage:"The chat state to continue, or null to start a new chat and return the state" local:"true" `
69+ ForceChat bool `usage:"Force an interactive chat session if even the top level tool is not a chat tool" local:"true" `
70+ ForceSequential bool `usage:"Force parallel calls to run sequentially" local:"true" `
7171 Workspace string `usage:"Directory to use for the workspace, if specified it will not be deleted on exit"`
7272 UI bool `usage:"Launch the UI" local:"true" name:"ui"`
7373 DisableTUI bool `usage:"Don't use chat TUI but instead verbose output" local:"true" name:"disable-tui"`
74+ SaveChatStateFile string `usage:"A file to save the chat state to so that a conversation can be resumed with --chat-state" local:"true"`
7475
7576 readData []byte
7677}
@@ -425,8 +426,18 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
425426 return err
426427 }
427428
428- if r .ChatState != "" {
429- resp , err := gptScript .Chat (cmd .Context (), r .ChatState , prg , gptOpt .Env , toolInput )
429+ var chatState string
430+ if r .ChatState != "" && r .ChatState != "null" && ! strings .HasPrefix (r .ChatState , "{" ) {
431+ data , err := os .ReadFile (r .ChatState )
432+ if err != nil {
433+ return fmt .Errorf ("reading %s: %w" , r .ChatState , err )
434+ }
435+ chatState = string (data )
436+ }
437+
438+ // This chat in a stateless mode
439+ if r .SaveChatStateFile == "-" || r .SaveChatStateFile == "stdout" {
440+ resp , err := gptScript .Chat (cmd .Context (), chatState , prg , gptOpt .Env , toolInput )
430441 if err != nil {
431442 return err
432443 }
@@ -446,11 +457,13 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
446457 CacheDir : r .CacheDir ,
447458 SubTool : r .SubTool ,
448459 Workspace : r .Workspace ,
460+ SaveChatStateFile : r .SaveChatStateFile ,
461+ ChatState : chatState ,
449462 })
450463 }
451- return chat .Start (cmd .Context (), nil , gptScript , func () (types.Program , error ) {
464+ return chat .Start (cmd .Context (), chatState , gptScript , func () (types.Program , error ) {
452465 return r .readProgram (ctx , gptScript , args )
453- }, gptOpt .Env , toolInput )
466+ }, gptOpt .Env , toolInput , r . SaveChatStateFile )
454467 }
455468
456469 s , err := gptScript .Run (cmd .Context (), prg , gptOpt .Env , toolInput )
0 commit comments