Skip to content

Commit a15d9ae

Browse files
committed
PR feedback
Signed-off-by: Grant Linville <grant@acorn.io>
1 parent 8672ec4 commit a15d9ae

File tree

7 files changed

+29
-53
lines changed

7 files changed

+29
-53
lines changed

pkg/engine/openapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (e *Engine) runOpenAPIRevamp(tool types.Tool, input string) (*Return, error
3737

3838
var res *Return
3939
switch command {
40-
case "list":
40+
case openapi.ListTool:
4141
t, err := openapi.Load(source)
4242
if err != nil {
4343
return nil, fmt.Errorf("failed to load OpenAPI file %s: %w", source, err)
@@ -56,7 +56,7 @@ func (e *Engine) runOpenAPIRevamp(tool types.Tool, input string) (*Return, error
5656
res = &Return{
5757
Result: ptr(string(opListJSON)),
5858
}
59-
case "get-schema":
59+
case openapi.GetSchemaTool:
6060
operation := gjson.Get(input, "operation").String()
6161

6262
if filter != "" && filter != openapi.NoFilter {
@@ -104,7 +104,7 @@ func (e *Engine) runOpenAPIRevamp(tool types.Tool, input string) (*Return, error
104104
res = &Return{
105105
Result: ptr(string(schemaJSON)),
106106
}
107-
case "run":
107+
case openapi.RunTool:
108108
operation := gjson.Get(input, "operation").String()
109109
args := gjson.Get(input, "args").String()
110110

pkg/loader/loader.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"time"
1616
"unicode/utf8"
1717

18-
"github.com/getkin/kin-openapi/openapi2"
19-
"github.com/getkin/kin-openapi/openapi2conv"
2018
"github.com/getkin/kin-openapi/openapi3"
2119
"github.com/gptscript-ai/gptscript/internal"
2220
"github.com/gptscript-ai/gptscript/pkg/assemble"
@@ -27,7 +25,6 @@ import (
2725
"github.com/gptscript-ai/gptscript/pkg/parser"
2826
"github.com/gptscript-ai/gptscript/pkg/system"
2927
"github.com/gptscript-ai/gptscript/pkg/types"
30-
kyaml "sigs.k8s.io/yaml"
3128
)
3229

3330
const CacheTimeout = time.Hour
@@ -157,33 +154,8 @@ func loadOpenAPI(prg *types.Program, data []byte) *openapi3.T {
157154
prg.OpenAPICache = map[string]any{}
158155
}
159156

160-
switch openapi.IsOpenAPI(data) {
161-
case 2:
162-
// Convert OpenAPI v2 to v3
163-
jsondata := data
164-
if !json.Valid(data) {
165-
jsondata, err = kyaml.YAMLToJSON(data)
166-
if err != nil {
167-
return nil
168-
}
169-
}
170-
171-
doc := &openapi2.T{}
172-
if err := doc.UnmarshalJSON(jsondata); err != nil {
173-
return nil
174-
}
175-
176-
openAPIDocument, err = openapi2conv.ToV3(doc)
177-
if err != nil {
178-
return nil
179-
}
180-
case 3:
181-
// Use OpenAPI v3 as is
182-
openAPIDocument, err = openapi3.NewLoader().LoadFromData(data)
183-
if err != nil {
184-
return nil
185-
}
186-
default:
157+
openAPIDocument, err = openapi.LoadFromBytes(data)
158+
if err != nil {
187159
return nil
188160
}
189161

pkg/loader/openapi.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func getOpenAPIToolsRevamp(t *openapi3.T, source, targetToolName string) ([]type
385385
Name: types.ToolNormalizer("list-operations-" + t.Info.Title),
386386
Description: fmt.Sprintf("List available operations for %s. Each of these operations is an OpenAPI operation. Run this tool before you do anything else.", t.Info.Title),
387387
},
388-
Instructions: fmt.Sprintf("%s list %s %s", types.OpenAPIPrefix, source, targetToolName),
388+
Instructions: fmt.Sprintf("%s %s %s %s", types.OpenAPIPrefix, openapi.ListTool, source, targetToolName),
389389
},
390390
Source: types.ToolSource{
391391
LineNo: 0,
@@ -411,7 +411,7 @@ func getOpenAPIToolsRevamp(t *openapi3.T, source, targetToolName string) ([]type
411411
},
412412
},
413413
},
414-
Instructions: fmt.Sprintf("%s get-schema %s %s", types.OpenAPIPrefix, source, targetToolName),
414+
Instructions: fmt.Sprintf("%s %s %s %s", types.OpenAPIPrefix, openapi.GetSchemaTool, source, targetToolName),
415415
},
416416
Source: types.ToolSource{
417417
LineNo: 1,
@@ -422,7 +422,7 @@ func getOpenAPIToolsRevamp(t *openapi3.T, source, targetToolName string) ([]type
422422
ToolDef: types.ToolDef{
423423
Parameters: types.Parameters{
424424
Name: types.ToolNormalizer("run-operation-" + t.Info.Title),
425-
Description: fmt.Sprintf("Run an operation for %s. You MUST call get-schema for the operation before you use this tool.", t.Info.Title),
425+
Description: fmt.Sprintf("Run an operation for %s. You MUST call %s for the operation before you use this tool.", t.Info.Title, openapi.GetSchemaTool),
426426
Arguments: &openapi3.Schema{
427427
Type: &openapi3.Types{openapi3.TypeObject},
428428
Properties: openapi3.Schemas{
@@ -445,7 +445,7 @@ func getOpenAPIToolsRevamp(t *openapi3.T, source, targetToolName string) ([]type
445445
},
446446
},
447447
},
448-
Instructions: fmt.Sprintf("%s run %s %s", types.OpenAPIPrefix, source, targetToolName),
448+
Instructions: fmt.Sprintf("%s %s %s %s", types.OpenAPIPrefix, openapi.RunTool, source, targetToolName),
449449
},
450450
}
451451

pkg/openapi/getschema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var (
3232
SupportedSecurityTypes = []string{"apiKey", "http"}
3333
)
3434

35+
const GetSchemaTool = "get-schema"
36+
3537
// GetSchema returns the JSONSchema and OperationInfo for a particular OpenAPI operation.
3638
// Return values in order: JSONSchema (string), OperationInfo, found (bool), error.
3739
func GetSchema(operationID, defaultHost string, t *openapi3.T) (string, OperationInfo, bool, error) {

pkg/openapi/list.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ type Operation struct {
1616
Summary string `json:"summary,omitempty"`
1717
}
1818

19-
const NoFilter = "<none>"
19+
const (
20+
ListTool = "list"
21+
NoFilter = "<none>"
22+
)
2023

2124
func List(t *openapi3.T, filter string) (OperationList, error) {
2225
operations := make(map[string]Operation)
@@ -28,14 +31,7 @@ func List(t *openapi3.T, filter string) (OperationList, error) {
2831
)
2932
if filter != "" && filter != NoFilter {
3033
if strings.Contains(filter, "*") {
31-
var filters []string
32-
if strings.Contains(filter, "|") {
33-
filters = strings.Split(filter, "|")
34-
} else {
35-
filters = []string{filter}
36-
}
37-
38-
match, err = MatchFilters(filters, operation.OperationID)
34+
match, err = MatchFilters(strings.Split(filter, "|"), operation.OperationID)
3935
if err != nil {
4036
return OperationList{}, err
4137
}

pkg/openapi/load.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func loadFromURL(source string) (*openapi3.T, error) {
3535
return nil, err
3636
}
3737

38-
return loadFromBytes(contents)
38+
return LoadFromBytes(contents)
3939
}
4040

4141
func loadFromFile(source string) (*openapi3.T, error) {
@@ -44,10 +44,10 @@ func loadFromFile(source string) (*openapi3.T, error) {
4444
return nil, err
4545
}
4646

47-
return loadFromBytes(contents)
47+
return LoadFromBytes(contents)
4848
}
4949

50-
func loadFromBytes(content []byte) (*openapi3.T, error) {
50+
func LoadFromBytes(content []byte) (*openapi3.T, error) {
5151
var (
5252
openAPIDocument *openapi3.T
5353
err error

pkg/openapi/run.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"golang.org/x/exp/maps"
1919
)
2020

21+
const RunTool = "run"
22+
2123
func Run(operationID, defaultHost, args string, t *openapi3.T, envs []string) (string, bool, error) {
2224
envMap := map[string]string{}
2325
for _, e := range envs {
@@ -145,11 +147,15 @@ func Run(operationID, defaultHost, args string, t *openapi3.T, envs []string) (s
145147
if err != nil {
146148
return "", false, fmt.Errorf("failed to make request: %w", err)
147149
}
148-
defer resp.Body.Close()
149150

150-
result, err := io.ReadAll(resp.Body)
151-
if err != nil {
152-
return "", false, fmt.Errorf("failed to read response: %w", err)
151+
var result []byte
152+
if resp.Body != nil {
153+
defer resp.Body.Close()
154+
155+
result, err = io.ReadAll(resp.Body)
156+
if err != nil {
157+
return "", false, fmt.Errorf("failed to read response: %w", err)
158+
}
153159
}
154160

155161
return string(result), true, nil

0 commit comments

Comments
 (0)