Skip to content

Commit 5c0f789

Browse files
Merge pull request #45 from thedadams/no-interface
chore: make GPTScript a struct rather than an interface
2 parents 8176fb2 + 7094c55 commit 5c0f789

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ import (
6262
)
6363

6464
func listTools(ctx context.Context) (string, error) {
65-
g, err := gptscript.NewGPTScript()
65+
g, err := gptscript.NewGPTScript(gptscript.GlobalOptions{})
6666
if err != nil {
6767
return "", err
6868
}
6969
defer g.Close()
70+
7071
return g.ListTools(ctx)
7172
}
7273
```
@@ -92,6 +93,7 @@ func listModels(ctx context.Context) ([]string, error) {
9293
return nil, err
9394
}
9495
defer g.Close()
96+
9597
return g.ListModels(ctx)
9698
}
9799
```

gptscript.go

+15-29
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,11 @@ var (
2626

2727
const relativeToBinaryPath = "<me>"
2828

29-
type GPTScript interface {
30-
Run(context.Context, string, Options) (*Run, error)
31-
Evaluate(context.Context, Options, ...ToolDef) (*Run, error)
32-
Parse(context.Context, string) ([]Node, error)
33-
ParseTool(context.Context, string) ([]Node, error)
34-
Version(context.Context) (string, error)
35-
Fmt(context.Context, []Node) (string, error)
36-
ListTools(context.Context) (string, error)
37-
ListModels(context.Context) ([]string, error)
38-
Confirm(context.Context, AuthResponse) error
39-
PromptResponse(context.Context, PromptResponse) error
40-
Close()
41-
}
42-
43-
type gptscript struct {
29+
type GPTScript struct {
4430
url string
4531
}
4632

47-
func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
33+
func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
4834
lock.Lock()
4935
defer lock.Unlock()
5036
gptscriptCount++
@@ -98,7 +84,7 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
9884
return nil, fmt.Errorf("failed to wait for gptscript to be ready: %w", err)
9985
}
10086
}
101-
return &gptscript{url: "http://" + serverURL}, nil
87+
return &GPTScript{url: "http://" + serverURL}, nil
10288
}
10389

10490
func waitForServerReady(ctx context.Context, serverURL string) error {
@@ -122,7 +108,7 @@ func waitForServerReady(ctx context.Context, serverURL string) error {
122108
}
123109
}
124110

125-
func (g *gptscript) Close() {
111+
func (g *GPTScript) Close() {
126112
lock.Lock()
127113
defer lock.Unlock()
128114
gptscriptCount--
@@ -133,7 +119,7 @@ func (g *gptscript) Close() {
133119
}
134120
}
135121

136-
func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
122+
func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
137123
return (&Run{
138124
url: g.url,
139125
requestPath: "evaluate",
@@ -143,7 +129,7 @@ func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
143129
}).NextChat(ctx, opts.Input)
144130
}
145131

146-
func (g *gptscript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
132+
func (g *GPTScript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
147133
return (&Run{
148134
url: g.url,
149135
requestPath: "run",
@@ -154,7 +140,7 @@ func (g *gptscript) Run(ctx context.Context, toolPath string, opts Options) (*Ru
154140
}
155141

156142
// Parse will parse the given file into an array of Nodes.
157-
func (g *gptscript) Parse(ctx context.Context, fileName string) ([]Node, error) {
143+
func (g *GPTScript) Parse(ctx context.Context, fileName string) ([]Node, error) {
158144
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName})
159145
if err != nil {
160146
return nil, err
@@ -173,7 +159,7 @@ func (g *gptscript) Parse(ctx context.Context, fileName string) ([]Node, error)
173159
}
174160

175161
// ParseTool will parse the given string into a tool.
176-
func (g *gptscript) ParseTool(ctx context.Context, toolDef string) ([]Node, error) {
162+
func (g *GPTScript) ParseTool(ctx context.Context, toolDef string) ([]Node, error) {
177163
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
178164
if err != nil {
179165
return nil, err
@@ -192,7 +178,7 @@ func (g *gptscript) ParseTool(ctx context.Context, toolDef string) ([]Node, erro
192178
}
193179

194180
// Fmt will format the given nodes into a string.
195-
func (g *gptscript) Fmt(ctx context.Context, nodes []Node) (string, error) {
181+
func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
196182
for _, node := range nodes {
197183
node.TextNode.combine()
198184
}
@@ -206,7 +192,7 @@ func (g *gptscript) Fmt(ctx context.Context, nodes []Node) (string, error) {
206192
}
207193

208194
// Version will return the output of `gptscript --version`
209-
func (g *gptscript) Version(ctx context.Context) (string, error) {
195+
func (g *GPTScript) Version(ctx context.Context) (string, error) {
210196
out, err := g.runBasicCommand(ctx, "version", nil)
211197
if err != nil {
212198
return "", err
@@ -216,7 +202,7 @@ func (g *gptscript) Version(ctx context.Context) (string, error) {
216202
}
217203

218204
// ListTools will list all the available tools.
219-
func (g *gptscript) ListTools(ctx context.Context) (string, error) {
205+
func (g *GPTScript) ListTools(ctx context.Context) (string, error) {
220206
out, err := g.runBasicCommand(ctx, "list-tools", nil)
221207
if err != nil {
222208
return "", err
@@ -226,7 +212,7 @@ func (g *gptscript) ListTools(ctx context.Context) (string, error) {
226212
}
227213

228214
// ListModels will list all the available models.
229-
func (g *gptscript) ListModels(ctx context.Context) ([]string, error) {
215+
func (g *GPTScript) ListModels(ctx context.Context) ([]string, error) {
230216
out, err := g.runBasicCommand(ctx, "list-models", nil)
231217
if err != nil {
232218
return nil, err
@@ -235,17 +221,17 @@ func (g *gptscript) ListModels(ctx context.Context) ([]string, error) {
235221
return strings.Split(strings.TrimSpace(out), "\n"), nil
236222
}
237223

238-
func (g *gptscript) Confirm(ctx context.Context, resp AuthResponse) error {
224+
func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
239225
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
240226
return err
241227
}
242228

243-
func (g *gptscript) PromptResponse(ctx context.Context, resp PromptResponse) error {
229+
func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
244230
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
245231
return err
246232
}
247233

248-
func (g *gptscript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
234+
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
249235
run := &Run{
250236
url: g.url,
251237
requestPath: requestPath,

gptscript_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/getkin/kin-openapi/openapi3"
1313
)
1414

15-
var g GPTScript
15+
var g *GPTScript
1616

1717
func TestMain(m *testing.M) {
1818
if os.Getenv("OPENAI_API_KEY") == "" && os.Getenv("GPTSCRIPT_URL") == "" {

0 commit comments

Comments
 (0)