@@ -3,6 +3,8 @@ package remote
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "net/url"
7
+ "os"
6
8
"slices"
7
9
"sort"
8
10
"strings"
@@ -87,6 +89,28 @@ func (c *Client) Supports(ctx context.Context, modelName string) (bool, error) {
87
89
return true , nil
88
90
}
89
91
92
+ func isHTTPURL (toolName string ) bool {
93
+ return strings .HasPrefix (toolName , "http://" ) ||
94
+ strings .HasPrefix (toolName , "https://" )
95
+ }
96
+
97
+ func (c * Client ) clientFromURL (apiURL string ) (* openai.Client , error ) {
98
+ parsed , err := url .Parse (apiURL )
99
+ if err != nil {
100
+ return nil , err
101
+ }
102
+ env := strings .ToUpper (strings .ReplaceAll (parsed .Hostname (), "." , "_" )) + "_API_KEY"
103
+ apiKey := os .Getenv (env )
104
+ if apiKey == "" {
105
+ apiKey = "<unset>"
106
+ }
107
+ return openai .NewClient (openai.Options {
108
+ BaseURL : apiURL ,
109
+ Cache : c .cache ,
110
+ APIKey : apiKey ,
111
+ })
112
+ }
113
+
90
114
func (c * Client ) load (ctx context.Context , toolName string ) (* openai.Client , error ) {
91
115
c .clientsLock .Lock ()
92
116
defer c .clientsLock .Unlock ()
@@ -96,6 +120,19 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
96
120
return client , nil
97
121
}
98
122
123
+ if c .clients == nil {
124
+ c .clients = make (map [string ]* openai.Client )
125
+ }
126
+
127
+ if isHTTPURL (toolName ) {
128
+ remoteClient , err := c .clientFromURL (toolName )
129
+ if err != nil {
130
+ return nil , err
131
+ }
132
+ c .clients [toolName ] = remoteClient
133
+ return remoteClient , nil
134
+ }
135
+
99
136
prg , err := loader .Program (ctx , toolName , "" )
100
137
if err != nil {
101
138
return nil , err
@@ -120,10 +157,6 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
120
157
return nil , err
121
158
}
122
159
123
- if c .clients == nil {
124
- c .clients = make (map [string ]* openai.Client )
125
- }
126
-
127
160
c .clients [toolName ] = client
128
161
return client , nil
129
162
}
0 commit comments