1
1
package cli
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"io"
6
7
"os"
8
+ "sort"
7
9
"strings"
8
10
9
11
"github.com/acorn-io/cmd"
@@ -41,17 +43,38 @@ type GPTScript struct {
41
43
Quiet * bool `usage:"No output logging (set --quiet=false to force on even when there is no TTY)" short:"q"`
42
44
Output string `usage:"Save output to a file, or - for stdout" short:"o"`
43
45
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
44
- SubTool string `usage:"Use tool of this name, not the first tool in file"`
45
- Assemble bool `usage:"Assemble tool to a single artifact, saved to --output" hidden:"true"`
46
- ListModels bool `usage:"List the models available and exit"`
47
- ListTools bool `usage:"List built-in tools and exit"`
48
- Server bool `usage:"Start server"`
49
- ListenAddress string `usage:"Server listen address" default:"127.0.0.1:9090"`
46
+ SubTool string `usage:"Use tool of this name, not the first tool in file" local:"true" `
47
+ Assemble bool `usage:"Assemble tool to a single artifact, saved to --output" hidden:"true" local:"true" `
48
+ ListModels bool `usage:"List the models available and exit" local:"true" `
49
+ ListTools bool `usage:"List built-in tools and exit" local:"true" `
50
+ Server bool `usage:"Start server" local:"true" `
51
+ ListenAddress string `usage:"Server listen address" default:"127.0.0.1:9090" local:"true" `
50
52
Chdir string `usage:"Change current working directory" short:"C"`
51
53
}
52
54
53
55
func New () * cobra.Command {
54
- return cmd .Command (& GPTScript {})
56
+ root := & GPTScript {}
57
+ return cmd .Command (root , & Eval {
58
+ gptscript : root ,
59
+ })
60
+ }
61
+
62
+ func (r * GPTScript ) NewRunContext (cmd * cobra.Command ) context.Context {
63
+ ctx := cmd .Context ()
64
+ if r .Confirm {
65
+ ctx = confirm .WithConfirm (ctx , confirm.TextPrompt {})
66
+ }
67
+ return ctx
68
+ }
69
+
70
+ func (r * GPTScript ) NewGPTScriptOpts () gptscript.Options {
71
+ return gptscript.Options {
72
+ Cache : cache .Options (r .CacheOptions ),
73
+ OpenAI : openai .Options (r .OpenAIOptions ),
74
+ Monitor : monitor .Options (r .DisplayOptions ),
75
+ Quiet : r .Quiet ,
76
+ Env : os .Environ (),
77
+ }
55
78
}
56
79
57
80
func (r * GPTScript ) Customize (cmd * cobra.Command ) {
@@ -74,16 +97,27 @@ func (r *GPTScript) Customize(cmd *cobra.Command) {
74
97
}
75
98
}
76
99
77
- func (r * GPTScript ) listTools () error {
100
+ func (r * GPTScript ) listTools (ctx context.Context , gptScript * gptscript.GPTScript , prg types.Program ) error {
101
+ tools := gptScript .ListTools (ctx , prg )
102
+ sort .Slice (tools , func (i , j int ) bool {
103
+ return tools [i ].Name < tools [j ].Name
104
+ })
78
105
var lines []string
79
- for _ , tool := range builtin .ListTools () {
106
+ for _ , tool := range tools {
107
+ if tool .Name == "" {
108
+ tool .Name = prg .Name
109
+ }
110
+
111
+ // Don't print instructions
112
+ tool .Instructions = ""
113
+
80
114
lines = append (lines , tool .String ())
81
115
}
82
116
fmt .Println (strings .Join (lines , "\n ---\n " ))
83
117
return nil
84
118
}
85
119
86
- func (r * GPTScript ) Pre (* cobra.Command , []string ) error {
120
+ func (r * GPTScript ) PersistentPre (* cobra.Command , []string ) error {
87
121
// chdir as soon as possible
88
122
if r .Chdir != "" {
89
123
if err := os .Chdir (r .Chdir ); err != nil {
@@ -111,10 +145,7 @@ func (r *GPTScript) Pre(*cobra.Command, []string) error {
111
145
mvl .SetError ()
112
146
}
113
147
}
114
- return nil
115
- }
116
148
117
- func (r * GPTScript ) Run (cmd * cobra.Command , args []string ) error {
118
149
if r .Color != nil {
119
150
color .NoColor = ! * r .Color
120
151
}
@@ -123,14 +154,64 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
123
154
log .Infof ("WARNING: Changing the default model can have unknown behavior for existing tools. Use the model field per tool instead." )
124
155
}
125
156
126
- gptOpt := gptscript.Options {
127
- Cache : cache .Options (r .CacheOptions ),
128
- OpenAI : openai .Options (r .OpenAIOptions ),
129
- Monitor : monitor .Options (r .DisplayOptions ),
130
- Quiet : r .Quiet ,
131
- Env : os .Environ (),
157
+ return nil
158
+ }
159
+
160
+ func (r * GPTScript ) listModels (ctx context.Context , gptScript * gptscript.GPTScript , args []string ) error {
161
+ models , err := gptScript .ListModels (ctx , args ... )
162
+ if err != nil {
163
+ return err
164
+ }
165
+ fmt .Println (strings .Join (models , "\n " ))
166
+ return nil
167
+ }
168
+
169
+ func (r * GPTScript ) readProgram (ctx context.Context , args []string ) (prg types.Program , err error ) {
170
+ if len (args ) == 0 {
171
+ return
132
172
}
133
173
174
+ if args [0 ] == "-" {
175
+ data , err := io .ReadAll (os .Stdin )
176
+ if err != nil {
177
+ return prg , err
178
+ }
179
+ prg , err = loader .ProgramFromSource (ctx , string (data ), r .SubTool )
180
+ if err != nil {
181
+ return prg , err
182
+ }
183
+ }
184
+
185
+ return loader .Program (ctx , args [0 ], r .SubTool )
186
+ }
187
+
188
+ func (r * GPTScript ) PrintOutput (toolInput , toolOutput string ) (err error ) {
189
+ if r .Output != "" {
190
+ err = os .WriteFile (r .Output , []byte (toolOutput ), 0644 )
191
+ if err != nil {
192
+ return err
193
+ }
194
+ } else {
195
+ if ! * r .Quiet {
196
+ if toolInput != "" {
197
+ _ , _ = fmt .Fprint (os .Stderr , "\n INPUT:\n \n " )
198
+ _ , _ = fmt .Fprintln (os .Stderr , toolInput )
199
+ }
200
+ _ , _ = fmt .Fprint (os .Stderr , "\n OUTPUT:\n \n " )
201
+ }
202
+ fmt .Print (toolOutput )
203
+ if ! strings .HasSuffix (toolOutput , "\n " ) {
204
+ fmt .Println ()
205
+ }
206
+ }
207
+
208
+ return
209
+ }
210
+
211
+ func (r * GPTScript ) Run (cmd * cobra.Command , args []string ) error {
212
+ gptOpt := r .NewGPTScriptOpts ()
213
+ ctx := cmd .Context ()
214
+
134
215
if r .Server {
135
216
s , err := server .New (& server.Options {
136
217
ListenAddress : r .ListenAddress ,
@@ -140,7 +221,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
140
221
return err
141
222
}
142
223
defer s .Close ()
143
- return s .Start (cmd . Context () )
224
+ return s .Start (ctx )
144
225
}
145
226
146
227
gptScript , err := gptscript .New (& gptOpt )
@@ -150,42 +231,22 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
150
231
defer gptScript .Close ()
151
232
152
233
if r .ListModels {
153
- models , err := gptScript . ListModels ( cmd . Context (), args ... )
154
- if err != nil {
155
- return err
156
- }
157
- fmt . Println ( strings . Join ( models , " \n " ))
158
- return nil
234
+ return r . listModels ( ctx , gptScript , args )
235
+ }
236
+
237
+ prg , err := r . readProgram ( ctx , args )
238
+ if err != nil {
239
+ return err
159
240
}
160
241
161
242
if r .ListTools {
162
- return r .listTools ()
243
+ return r .listTools (ctx , gptScript , prg )
163
244
}
164
245
165
246
if len (args ) == 0 {
166
247
return cmd .Help ()
167
248
}
168
249
169
- var (
170
- prg types.Program
171
- )
172
-
173
- if args [0 ] == "-" {
174
- data , err := io .ReadAll (os .Stdin )
175
- if err != nil {
176
- return err
177
- }
178
- prg , err = loader .ProgramFromSource (cmd .Context (), string (data ), r .SubTool )
179
- if err != nil {
180
- return err
181
- }
182
- } else {
183
- prg , err = loader .Program (cmd .Context (), args [0 ], r .SubTool )
184
- if err != nil {
185
- return err
186
- }
187
- }
188
-
189
250
if r .Assemble {
190
251
var out io.Writer = os .Stdout
191
252
if r .Output != "" && r .Output != "-" {
@@ -205,33 +266,10 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
205
266
return err
206
267
}
207
268
208
- ctx := cmd .Context ()
209
- if r .Confirm {
210
- ctx = confirm .WithConfirm (ctx , confirm.TextPrompt {})
211
- }
212
- s , err := gptScript .Run (ctx , prg , os .Environ (), toolInput )
269
+ s , err := gptScript .Run (r .NewRunContext (cmd ), prg , os .Environ (), toolInput )
213
270
if err != nil {
214
271
return err
215
272
}
216
273
217
- if r .Output != "" {
218
- err = os .WriteFile (r .Output , []byte (s ), 0644 )
219
- if err != nil {
220
- return err
221
- }
222
- } else {
223
- if ! * r .Quiet {
224
- if toolInput != "" {
225
- _ , _ = fmt .Fprint (os .Stderr , "\n INPUT:\n \n " )
226
- _ , _ = fmt .Fprintln (os .Stderr , toolInput )
227
- }
228
- _ , _ = fmt .Fprint (os .Stderr , "\n OUTPUT:\n \n " )
229
- }
230
- fmt .Print (s )
231
- if ! strings .HasSuffix (s , "\n " ) {
232
- fmt .Println ()
233
- }
234
- }
235
-
236
- return nil
274
+ return r .PrintOutput (toolInput , s )
237
275
}
0 commit comments