Skip to content
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

feat: add credential management #65

Merged
merged 15 commits into from
Sep 19, 2024
27 changes: 27 additions & 0 deletions credentials.go
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"`
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ module github.com/gptscript-ai/go-gptscript

go 1.23.0

require github.com/getkin/kin-openapi v0.124.0
require (
github.com/getkin/kin-openapi v0.124.0
github.com/stretchr/testify v1.8.4
Copy link
Collaborator

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.

Copy link
Contributor

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?

)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
90 changes: 77 additions & 13 deletions gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt
disableCache = disableCache || opt.DisableCache
}

out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
if err != nil {
return nil, err
}
Expand All @@ -180,7 +180,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt

// ParseContent will parse the given string into a tool.
func (g *GPTScript) ParseContent(ctx context.Context, toolDef string) ([]Node, error) {
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
if err != nil {
return nil, err
}
Expand All @@ -203,7 +203,7 @@ func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
node.TextNode.combine()
}

out, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
out, _, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
if err != nil {
return "", err
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo
}
}

out, err := g.runBasicCommand(ctx, "load", payload)
out, _, err := g.runBasicCommand(ctx, "load", payload)
if err != nil {
return nil, err
}
Expand All @@ -260,7 +260,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo

// Version will return the output of `gptscript --version`
func (g *GPTScript) Version(ctx context.Context) (string, error) {
out, err := g.runBasicCommand(ctx, "version", nil)
out, _, err := g.runBasicCommand(ctx, "version", nil)
if err != nil {
return "", err
}
Expand All @@ -285,7 +285,7 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
o.Providers = append(o.Providers, g.globalOpts.DefaultModelProvider)
}

out, err := g.runBasicCommand(ctx, "list-models", map[string]any{
out, _, err := g.runBasicCommand(ctx, "list-models", map[string]any{
"providers": o.Providers,
"env": g.globalOpts.Env,
"credentialOverrides": o.CredentialOverrides,
Expand All @@ -298,16 +298,80 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
}

func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
_, _, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
return err
}

func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
_, _, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
return err
}

func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
func (g *GPTScript) ListCredentials(ctx context.Context, credCtxs []string, allContexts bool) ([]Credential, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can credCtxs and allContext be added to an Options struct. If no options are passed is should do the same thing as gptscript creds which I really don't know if that is "all contexts" or what.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

req := CredentialRequest{}
if allContexts {
req.AllContexts = true
} else {
req.Context = credCtxs
}

out, _, err := g.runBasicCommand(ctx, "credentials", req)
if err != nil {
return nil, err
}

var creds []Credential
if err = json.Unmarshal([]byte(out), &creds); err != nil {
return nil, err
}
return creds, nil
}

func (g *GPTScript) CreateCredential(ctx context.Context, cred Credential) error {
credJSON, err := json.Marshal(cred)
if err != nil {
return fmt.Errorf("failed to marshal credential: %w", err)
}

_, _, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
return err
}

func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, name string) (Credential, error) {
out, _, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
Context: credCtxs,
Name: name,
})
if err != nil {
return Credential{}, err
}

var cred Credential
if err = json.Unmarshal([]byte(out), &cred); err != nil {
return Credential{}, err
}
return cred, nil
}

// DeleteCredential will delete the credential with the given name in the given context.
// A return value of false, nil indicates that the credential was not found.
// false, non-nil error indicates a different error when trying to delete.
// true, nil indicates a successful deletion.
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) (bool, error) {
_, code, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
Context: []string{credCtx}, // Only one context can be specified for delete operations
Name: name,
})
if err != nil {
if code == 404 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
if code == 404 {
if code == http.StatusNotFound {

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

return false, nil
}
return false, err
}
return true, nil
}

func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, int, error) {
run := &Run{
url: g.url,
requestPath: requestPath,
Expand All @@ -316,18 +380,18 @@ func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, bod
}

if err := run.request(ctx, body); err != nil {
return "", err
return "", run.responseCode, err
}

out, err := run.Text()
if err != nil {
return "", err
return "", run.responseCode, err
}
if run.err != nil {
return run.ErrorOutput(), run.err
return run.ErrorOutput(), run.responseCode, run.err
}

return out, nil
return out, run.responseCode, nil
}

func getCommand() string {
Expand Down
38 changes: 38 additions & 0 deletions gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package gptscript
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

var g *GPTScript
Expand Down Expand Up @@ -1448,3 +1451,38 @@ func TestLoadTools(t *testing.T) {
t.Errorf("Unexpected name: %s", prg.Name)
}
}

func TestCredentials(t *testing.T) {

Choose a reason for hiding this comment

The 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(), []string{"testing"}, false)
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
found, err := g.DeleteCredential(context.Background(), "testing", name)
require.NoError(t, err)
require.True(t, found)
}
11 changes: 11 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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.
Expand Down Expand Up @@ -235,6 +236,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")
Expand Down Expand Up @@ -325,6 +327,15 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {

done, _ = out["done"].(bool)
r.rawOutput = out
case []any:

Choose a reason for hiding this comment

The 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
Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Expand Down
Loading