Skip to content

Commit 18426fd

Browse files
committed
fix: don't prompt for creds for local models
Signed-off-by: Grant Linville <[email protected]>
1 parent 53f7fbd commit 18426fd

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

pkg/credentials/credential.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ func (c Credential) toDockerAuthConfig() (types.AuthConfig, error) {
4949

5050
func credentialFromDockerAuthConfig(authCfg types.AuthConfig) (Credential, error) {
5151
var cred Credential
52-
if err := json.Unmarshal([]byte(authCfg.Password), &cred); err != nil || len(cred.Env) == 0 {
53-
// Legacy: try unmarshalling into just an env map
54-
var env map[string]string
55-
if err := json.Unmarshal([]byte(authCfg.Password), &env); err != nil {
56-
return Credential{}, err
52+
if authCfg.Password != "" {
53+
if err := json.Unmarshal([]byte(authCfg.Password), &cred); err != nil || len(cred.Env) == 0 {
54+
// Legacy: try unmarshalling into just an env map
55+
var env map[string]string
56+
if err := json.Unmarshal([]byte(authCfg.Password), &env); err != nil {
57+
return Credential{}, err
58+
}
59+
cred.Env = env
5760
}
58-
cred.Env = env
5961
}
6062

6163
// We used to hardcode the username as "gptscript" before CredentialType was introduced, so

pkg/credentials/helper.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package credentials
22

33
import (
44
"errors"
5+
"net/url"
56
"regexp"
67
"strings"
78

@@ -71,9 +72,18 @@ func (h *HelperStore) GetAll() (map[string]types.AuthConfig, error) {
7172
contextPieces := strings.Split(ctx, ":")
7273
if len(contextPieces) > 1 {
7374
possiblePortNumber := contextPieces[len(contextPieces)-1]
74-
if regexp.MustCompile(`\d+$`).MatchString(possiblePortNumber) {
75+
if regexp.MustCompile(`^\d+$`).MatchString(possiblePortNumber) {
7576
// port number confirmed
76-
toolName = toolName + ":" + possiblePortNumber
77+
toolURL, err := url.Parse(toolName)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
// Save the path so we can put it back after removing it.
83+
path := toolURL.Path
84+
toolURL.Path = ""
85+
86+
toolName = toolURL.String() + ":" + possiblePortNumber + path
7787
ctx = strings.TrimSuffix(ctx, ":"+possiblePortNumber)
7888
}
7989
}

pkg/remote/remote.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (c *Client) clientFromURL(ctx context.Context, apiURL string) (*openai.Clie
108108
env := "GPTSCRIPT_PROVIDER_" + env2.ToEnvLike(parsed.Hostname()) + "_API_KEY"
109109
key := os.Getenv(env)
110110

111-
if key == "" {
111+
if key == "" && !isLocalhost(apiURL) {
112112
var err error
113113
key, err = c.retrieveAPIKey(ctx, env, apiURL)
114114
if err != nil {
@@ -179,3 +179,8 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
179179
func (c *Client) retrieveAPIKey(ctx context.Context, env, url string) (string, error) {
180180
return prompt.GetModelProviderCredential(ctx, c.credStore, url, env, fmt.Sprintf("Please provide your API key for %s", url), append(gcontext.GetEnv(ctx), c.envs...))
181181
}
182+
183+
func isLocalhost(url string) bool {
184+
return strings.HasPrefix(url, "http://localhost") || strings.HasPrefix(url, "http://127.0.0.1") ||
185+
strings.HasPrefix(url, "https://localhost") || strings.HasPrefix(url, "https://127.0.0.1")
186+
}

pkg/types/tool.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"path/filepath"
8+
"regexp"
89
"slices"
910
"sort"
1011
"strings"
@@ -283,6 +284,10 @@ func ParseCredentialArgs(toolName string, input string) (string, string, map[str
283284
fields = fields[2:]
284285
}
285286

287+
if alias != "" && !isAlphaNumeric(alias) {
288+
return "", "", nil, fmt.Errorf("credential alias must be alphanumeric")
289+
}
290+
286291
if len(fields) == 0 { // Nothing left, so just return
287292
return originalName, alias, nil, nil
288293
}
@@ -780,3 +785,7 @@ func FirstSet[T comparable](in ...T) (result T) {
780785
}
781786
return
782787
}
788+
789+
func isAlphaNumeric(s string) bool {
790+
return regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString(s)
791+
}

0 commit comments

Comments
 (0)