-
Notifications
You must be signed in to change notification settings - Fork 297
fix: don't prompt for creds for local models #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package credentials | |
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
|
@@ -71,9 +72,18 @@ func (h *HelperStore) GetAll() (map[string]types.AuthConfig, error) { | |
contextPieces := strings.Split(ctx, ":") | ||
if len(contextPieces) > 1 { | ||
possiblePortNumber := contextPieces[len(contextPieces)-1] | ||
if regexp.MustCompile(`\d+$`).MatchString(possiblePortNumber) { | ||
if regexp.MustCompile(`^\d+$`).MatchString(possiblePortNumber) { | ||
// port number confirmed | ||
toolName = toolName + ":" + possiblePortNumber | ||
toolURL, err := url.Parse(toolName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Save the path so we can put it back after removing it. | ||
path := toolURL.Path | ||
toolURL.Path = "" | ||
|
||
toolName = toolURL.String() + ":" + possiblePortNumber + path | ||
ctx = strings.TrimSuffix(ctx, ":"+possiblePortNumber) | ||
Comment on lines
+77
to
87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes a bug where we were putting the port after the path. |
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ func (c *Client) clientFromURL(ctx context.Context, apiURL string) (*openai.Clie | |
env := "GPTSCRIPT_PROVIDER_" + env2.ToEnvLike(parsed.Hostname()) + "_API_KEY" | ||
key := os.Getenv(env) | ||
|
||
if key == "" { | ||
if key == "" && !isLocalhost(apiURL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we no longer look for the credential in the store if the model is running locally. Not sure whether we care to support IPv6 ( |
||
var err error | ||
key, err = c.retrieveAPIKey(ctx, env, apiURL) | ||
if err != nil { | ||
|
@@ -179,3 +179,8 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err | |
func (c *Client) retrieveAPIKey(ctx context.Context, env, url string) (string, error) { | ||
return prompt.GetModelProviderCredential(ctx, c.credStore, url, env, fmt.Sprintf("Please provide your API key for %s", url), append(gcontext.GetEnv(ctx), c.envs...)) | ||
} | ||
|
||
func isLocalhost(url string) bool { | ||
return strings.HasPrefix(url, "http://localhost") || strings.HasPrefix(url, "http://127.0.0.1") || | ||
strings.HasPrefix(url, "https://localhost") || strings.HasPrefix(url, "https://127.0.0.1") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"sort" | ||
"strings" | ||
|
@@ -283,6 +284,10 @@ func ParseCredentialArgs(toolName string, input string) (string, string, map[str | |
fields = fields[2:] | ||
} | ||
|
||
if alias != "" && !isAlphaNumeric(alias) { | ||
return "", "", nil, fmt.Errorf("credential alias must be alphanumeric") | ||
} | ||
Comment on lines
+287
to
+289
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to add a constraint that a credential alias needs to be alphanumeric. This is to avoid interfering with credentials for model providers and such. |
||
|
||
if len(fields) == 0 { // Nothing left, so just return | ||
return originalName, alias, nil, nil | ||
} | ||
|
@@ -780,3 +785,7 @@ func FirstSet[T comparable](in ...T) (result T) { | |
} | ||
return | ||
} | ||
|
||
func isAlphaNumeric(s string) bool { | ||
return regexp.MustCompile(`^[a-zA-Z0-9_.]+$`).MatchString(s) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a bug where we would try to unmarshal an empty string.