-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add credential management #65
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
c7d8d7f
a28b579
ebc128c
2560982
3e9d2ba
5960c54
6ed61ee
a778693
354587a
8d1d053
accfe59
512ec19
19f35f5
1c0e3ec
114eb11
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gptscript | ||
|
||
import "time" | ||
|
||
type CredentialType string | ||
|
||
const ( | ||
CredentialTypeTool CredentialType = "tool" | ||
CredentialTypeModelProvider CredentialType = "modelProvider" | ||
) | ||
|
||
type Credential struct { | ||
Context string `json:"context"` | ||
ToolName string `json:"toolName"` | ||
Type CredentialType `json:"type"` | ||
Env map[string]string `json:"env"` | ||
Ephemeral bool `json:"ephemeral,omitempty"` | ||
ExpiresAt *time.Time `json:"expiresAt"` | ||
RefreshToken string `json:"refreshToken"` | ||
} | ||
|
||
type CredentialRequest struct { | ||
Content string `json:"content"` | ||
AllContexts bool `json:"allContexts"` | ||
Context []string `json:"context"` | ||
Name string `json:"name"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ package gptscript | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var g *GPTScript | ||
|
@@ -1448,3 +1452,44 @@ func TestLoadTools(t *testing.T) { | |
t.Errorf("Unexpected name: %s", prg.Name) | ||
} | ||
} | ||
|
||
func TestCredentials(t *testing.T) { | ||
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. 👍 |
||
// We will test in the following order of create, list, reveal, delete. | ||
name := "test-" + strconv.Itoa(rand.Int()) | ||
if len(name) > 20 { | ||
name = name[:20] | ||
} | ||
|
||
// Create | ||
err := g.CreateCredential(context.Background(), Credential{ | ||
Context: "testing", | ||
ToolName: name, | ||
Type: CredentialTypeTool, | ||
Env: map[string]string{"ENV": "testing"}, | ||
RefreshToken: "my-refresh-token", | ||
}) | ||
require.NoError(t, err) | ||
|
||
// List | ||
creds, err := g.ListCredentials(context.Background(), ListCredentialsOptions{ | ||
CredentialContexts: []string{"testing"}, | ||
}) | ||
require.NoError(t, err) | ||
require.GreaterOrEqual(t, len(creds), 1) | ||
|
||
// Reveal | ||
cred, err := g.RevealCredential(context.Background(), []string{"testing"}, name) | ||
require.NoError(t, err) | ||
require.Contains(t, cred.Env, "ENV") | ||
require.Equal(t, cred.Env["ENV"], "testing") | ||
require.Equal(t, cred.RefreshToken, "my-refresh-token") | ||
|
||
// Delete | ||
err = g.DeleteCredential(context.Background(), "testing", name) | ||
require.NoError(t, err) | ||
|
||
// Delete again and make sure we get a NotFoundError | ||
err = g.DeleteCredential(context.Background(), "testing", name) | ||
require.Error(t, err) | ||
require.True(t, errors.As(err, &ErrNotFound{})) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,14 @@ import ( | |
|
||
var errAbortRun = errors.New("run aborted") | ||
|
||
type ErrNotFound struct { | ||
Message string | ||
} | ||
|
||
func (e ErrNotFound) Error() string { | ||
return e.Message | ||
} | ||
|
||
type Run struct { | ||
url, requestPath, toolPath string | ||
tools []ToolDef | ||
|
@@ -36,6 +44,7 @@ type Run struct { | |
output, errput string | ||
events chan Frame | ||
lock sync.Mutex | ||
responseCode int | ||
} | ||
|
||
// Text returns the text output of the gptscript. It blocks until the output is ready. | ||
|
@@ -60,6 +69,11 @@ func (r *Run) State() RunState { | |
// Err returns the error that caused the gptscript to fail, if any. | ||
func (r *Run) Err() error { | ||
if r.err != nil { | ||
if r.responseCode == http.StatusNotFound { | ||
return ErrNotFound{ | ||
Message: fmt.Sprintf("run encountered an error: %s", r.errput), | ||
} | ||
} | ||
return fmt.Errorf("run encountered an error: %w with error output: %s", r.err, r.errput) | ||
} | ||
return nil | ||
|
@@ -235,6 +249,7 @@ func (r *Run) request(ctx context.Context, payload any) (err error) { | |
return r.err | ||
} | ||
|
||
r.responseCode = resp.StatusCode | ||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest { | ||
r.state = Error | ||
r.err = fmt.Errorf("run encountered an error") | ||
|
@@ -325,6 +340,15 @@ func (r *Run) request(ctx context.Context, payload any) (err error) { | |
|
||
done, _ = out["done"].(bool) | ||
r.rawOutput = out | ||
case []any: | ||
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. Is this checking if credential returns an array of objects? if so then it seems fine to me |
||
b, err := json.Marshal(out) | ||
if err != nil { | ||
r.state = Error | ||
r.err = fmt.Errorf("failed to process stdout: %w", err) | ||
return | ||
} | ||
|
||
r.output = string(b) | ||
Comment on lines
+343
to
+351
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. Is this correct? Maybe I should just return a string in the SDK instead of the full slice? |
||
default: | ||
r.state = Error | ||
r.err = fmt.Errorf("failed to process stdout, invalid type: %T", out) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: echocred | ||
credential: mycredentialtool as testcred | ||
|
||
#!/usr/bin/env bash | ||
|
||
echo $VALUE | ||
|
||
--- | ||
name: mycredentialtool | ||
|
||
#!sys.echo | ||
|
||
{"env":{"VALUE":"hello"}} |
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.
nit: I would prefer to not have this.
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.
nit: why? doesn't everyone use testify? What else would you use?