66 "io"
77 "os"
88 "sort"
9+ "strconv"
910 "strings"
1011
1112 "github.com/acorn-io/cmd"
@@ -50,6 +51,8 @@ type GPTScript struct {
5051 Server bool `usage:"Start server" local:"true"`
5152 ListenAddress string `usage:"Server listen address" default:"127.0.0.1:9090" local:"true"`
5253 Chdir string `usage:"Change current working directory" short:"C"`
54+ Daemon bool `usage:"Run tool as a daemon" local:"true" hidden:"true"`
55+ Ports string `usage:"The port range to use for ephemeral daemon ports (ex: 11000-12000)" hidden:"true"`
5356}
5457
5558func New () * cobra.Command {
@@ -67,14 +70,33 @@ func (r *GPTScript) NewRunContext(cmd *cobra.Command) context.Context {
6770 return ctx
6871}
6972
70- func (r * GPTScript ) NewGPTScriptOpts () gptscript.Options {
71- return gptscript.Options {
73+ func (r * GPTScript ) NewGPTScriptOpts () ( gptscript.Options , error ) {
74+ opts := gptscript.Options {
7275 Cache : cache .Options (r .CacheOptions ),
7376 OpenAI : openai .Options (r .OpenAIOptions ),
7477 Monitor : monitor .Options (r .DisplayOptions ),
7578 Quiet : r .Quiet ,
7679 Env : os .Environ (),
7780 }
81+
82+ if r .Ports != "" {
83+ start , end , _ := strings .Cut (r .Ports , "-" )
84+ startNum , err := strconv .ParseInt (strings .TrimSpace (start ), 10 , 64 )
85+ if err != nil {
86+ return gptscript.Options {}, fmt .Errorf ("invalid port range: %s" , r .Ports )
87+ }
88+ var endNum int64
89+ if end != "" {
90+ endNum , err = strconv .ParseInt (strings .TrimSpace (end ), 10 , 64 )
91+ if err != nil {
92+ return gptscript.Options {}, fmt .Errorf ("invalid port range: %s" , r .Ports )
93+ }
94+ }
95+ opts .Runner .StartPort = startNum
96+ opts .Runner .EndPort = endNum
97+ }
98+
99+ return opts , nil
78100}
79101
80102func (r * GPTScript ) Customize (cmd * cobra.Command ) {
@@ -205,8 +227,12 @@ func (r *GPTScript) PrintOutput(toolInput, toolOutput string) (err error) {
205227 return
206228}
207229
208- func (r * GPTScript ) Run (cmd * cobra.Command , args []string ) error {
209- gptOpt := r .NewGPTScriptOpts ()
230+ func (r * GPTScript ) Run (cmd * cobra.Command , args []string ) (retErr error ) {
231+ gptOpt , err := r .NewGPTScriptOpts ()
232+ if err != nil {
233+ return err
234+ }
235+
210236 ctx := cmd .Context ()
211237
212238 if r .Server {
@@ -236,6 +262,15 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
236262 return err
237263 }
238264
265+ if r .Daemon {
266+ prg = prg .SetBlocking ()
267+ defer func () {
268+ if retErr == nil {
269+ <- ctx .Done ()
270+ }
271+ }()
272+ }
273+
239274 if r .ListTools {
240275 return r .listTools (ctx , gptScript , prg )
241276 }
0 commit comments