Skip to content

enhance: add support for args and aliases to credential tools #433

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

Merged
merged 9 commits into from
Jun 6, 2024

Conversation

g-linville
Copy link
Member

@g-linville g-linville commented Jun 4, 2024

This adds support for aliases and arguments in credential tools. Read the docs changes in the PR to find out how it works.

@@ -136,7 +136,7 @@ func isParam(line string, tool *types.Tool) (_ bool, err error) {
return false, err
}
case "credentials", "creds", "credential", "cred":
tool.Parameters.Credentials = append(tool.Parameters.Credentials, csv(strings.ToLower(value))...)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no need to be converting it to lowercase here.

@g-linville g-linville force-pushed the credentials-with-args branch from 04e1e48 to 2479f1f Compare June 4, 2024 21:58
@cloudnautique
Copy link
Contributor

Can this be a built-in credential tool, or are we going to publish a basic one to handle this?

@g-linville
Copy link
Member Author

Can this be a built-in credential tool, or are we going to publish a basic one to handle this?

@cloudnautique we're going to have a generic credential tool that will be a separate repo. This PR is just the changes needed to support such a thing, plus a few other enhancements.

@g-linville g-linville force-pushed the credentials-with-args branch from 2479f1f to 00383b2 Compare June 5, 2024 02:04
@g-linville g-linville marked this pull request as ready for review June 5, 2024 02:04
@g-linville g-linville force-pushed the credentials-with-args branch 2 times, most recently from f4cb837 to cb638ce Compare June 6, 2024 00:43
@g-linville g-linville force-pushed the credentials-with-args branch from cb638ce to c9e6fea Compare June 6, 2024 14:07
Comment on lines 132 to 150
originalName, alias, args, err := ParseCredentialArgs(tt.toolName, tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseCredentialArgs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if originalName != tt.expectedName {
t.Errorf("ParseCredentialArgs() originalName = %v, expectedName %v", originalName, tt.expectedName)
}
if alias != tt.expectedAlias {
t.Errorf("ParseCredentialArgs() alias = %v, expectedAlias %v", alias, tt.expectedAlias)
}
if len(args) != len(tt.expectedArgs) {
t.Errorf("ParseCredentialArgs() args = %v, expectedArgs %v", args, tt.expectedArgs)
}
for k, v := range tt.expectedArgs {
if args[k] != v {
t.Errorf("ParseCredentialArgs() args[%s] = %v, expectedArgs[%s] %v", k, args[k], k, v)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the github.com/stretchr/testify/assert and github.com/stretchr/testify/require packages:

Suggested change
originalName, alias, args, err := ParseCredentialArgs(tt.toolName, tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseCredentialArgs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if originalName != tt.expectedName {
t.Errorf("ParseCredentialArgs() originalName = %v, expectedName %v", originalName, tt.expectedName)
}
if alias != tt.expectedAlias {
t.Errorf("ParseCredentialArgs() alias = %v, expectedAlias %v", alias, tt.expectedAlias)
}
if len(args) != len(tt.expectedArgs) {
t.Errorf("ParseCredentialArgs() args = %v, expectedArgs %v", args, tt.expectedArgs)
}
for k, v := range tt.expectedArgs {
if args[k] != v {
t.Errorf("ParseCredentialArgs() args[%s] = %v, expectedArgs[%s] %v", k, args[k], k, v)
}
}
originalName, alias, args, err := ParseCredentialArgs(tt.toolName, tt.input)
// Require halts the test immediately when an assertion fails
if tt.wantErr {
require.Error(t, err, "expected an error but got none")
return
}
require.NoError(t, err, "did not expect an error but got one")
require.Equal(t, tt.expectedName, originalName, "unexpected original name")
require.Equal(t, tt.expectedAlias, alias, "unexpected alias")
require.Equal(t, len(tt.expectedArgs), len(args), "unexpected number of args")
for k, v := range tt.expectedArgs {
// Assert marks the test as failed when an assertion fails, but doesn't halt, allowing the test to progress further and identify more potential issues
assert.Equal(t, v, args[k], "unexpected value for args[%s]", k)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thanks lol. I let Copilot generate basically all of this stuff and did not bother to think how it could be improved. Lazy me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -838,12 +848,21 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
return nil, fmt.Errorf("failed to find ID for tool %s", credToolName)
}

subCtx, err := callCtx.SubCall(callCtx.Ctx, "", credToolRefs[0].ToolID, "", engine.CredentialToolCategory) // leaving callID as "" will cause it to be set by the engine
input := ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
input := ""
var input string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Signed-off-by: Grant Linville <[email protected]>
@g-linville g-linville requested a review from njhale June 6, 2024 15:45
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
"github.com/stretchr/testify/require"
)

func TestParseCredentialArgs(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@g-linville g-linville merged commit 0ef0f82 into gptscript-ai:main Jun 6, 2024
3 checks passed
@g-linville g-linville deleted the credentials-with-args branch June 6, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants