@@ -3,6 +3,7 @@ package loader
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "net/url"
6
7
"slices"
7
8
"strings"
8
9
@@ -15,15 +16,26 @@ import (
15
16
// Each operation will become a tool definition.
16
17
// The tool's Instructions will be in the format "#!sys.openapi '{JSON Instructions}'",
17
18
// where the JSON Instructions are a JSON-serialized engine.OpenAPIInstructions struct.
18
- func getOpenAPITools (t * openapi3.T ) ([]types.Tool , error ) {
19
+ func getOpenAPITools (t * openapi3.T , defaultHost string ) ([]types.Tool , error ) {
20
+ // Determine the default server.
19
21
if len (t .Servers ) == 0 {
20
- return nil , fmt .Errorf ("no servers found in OpenAPI spec" )
22
+ if defaultHost != "" {
23
+ u , err := url .Parse (defaultHost )
24
+ if err != nil {
25
+ return nil , fmt .Errorf ("invalid default host URL: %w" , err )
26
+ }
27
+ u .Path = "/"
28
+ t .Servers = []* openapi3.Server {{URL : u .String ()}}
29
+ } else {
30
+ return nil , fmt .Errorf ("no servers found in OpenAPI spec" )
31
+ }
21
32
}
22
33
defaultServer , err := parseServer (t .Servers [0 ])
23
34
if err != nil {
24
35
return nil , err
25
36
}
26
37
38
+ // Generate a tool for each operation.
27
39
var (
28
40
toolNames []string
29
41
tools []types.Tool
@@ -80,10 +92,9 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
80
92
},
81
93
}
82
94
83
- toolNames = append (toolNames , tool .Parameters .Name )
84
-
85
- // Handle query, path, and header parameters
86
- for _ , param := range operation .Parameters {
95
+ // Handle query, path, and header parameters, based on the parameters for this operation
96
+ // and the parameters for this path.
97
+ for _ , param := range append (operation .Parameters , pathObj .Parameters ... ) {
87
98
arg := param .Value .Schema .Value
88
99
89
100
if arg .Description == "" {
@@ -136,10 +147,6 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
136
147
tool .Parameters .Arguments .Properties ["requestBodyContent" ] = & openapi3.SchemaRef {Value : arg }
137
148
break
138
149
}
139
-
140
- if bodyMIME == "" {
141
- return nil , fmt .Errorf ("no supported MIME types found for request body in operation %s" , operation .OperationID )
142
- }
143
150
}
144
151
145
152
// OpenAI will get upset if we have an object schema with no properties,
@@ -154,6 +161,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
154
161
return nil , err
155
162
}
156
163
164
+ // Register
165
+ toolNames = append (toolNames , tool .Parameters .Name )
157
166
tools = append (tools , tool )
158
167
operationNum ++
159
168
}
0 commit comments