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: simplify workspace API and add support for s3 #73

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package gptscript
// GlobalOptions allows specification of settings that are used for every call made.
// These options can be overridden by the corresponding Options.
type GlobalOptions struct {
URL string `json:"url"`
Token string `json:"token"`
OpenAIAPIKey string `json:"APIKey"`
OpenAIBaseURL string `json:"BaseURL"`
DefaultModel string `json:"DefaultModel"`
DefaultModelProvider string `json:"DefaultModelProvider"`
CacheDir string `json:"CacheDir"`
Env []string `json:"env"`
DatasetToolRepo string `json:"DatasetToolRepo"`
WorkspaceTool string `json:"WorkspaceTool"`
URL string `json:"url"`
Token string `json:"token"`
OpenAIAPIKey string `json:"APIKey"`
OpenAIBaseURL string `json:"BaseURL"`
DefaultModel string `json:"DefaultModel"`
DefaultModelProvider string `json:"DefaultModelProvider"`
CacheDir string `json:"CacheDir"`
Env []string `json:"env"`
DatasetToolRepo string `json:"DatasetToolRepo"`
WorkspaceTool string `json:"WorkspaceTool"`
WorkspaceDirectoryDataHome string `json:"WorkspaceDirectoryDataHome"`
}

func (g GlobalOptions) toEnv() []string {
Expand All @@ -29,6 +30,9 @@ func (g GlobalOptions) toEnv() []string {
if g.DefaultModelProvider != "" {
args = append(args, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL_PROVIDER="+g.DefaultModelProvider)
}
if g.WorkspaceDirectoryDataHome != "" {
args = append(args, "GPTSCRIPT_WORKSPACE_DIR="+g.WorkspaceDirectoryDataHome)
}

return args
}
Expand Down
147 changes: 33 additions & 114 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package gptscript
import (
"context"
"encoding/base64"
"encoding/json"
"strings"
)

func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string) (string, error) {
func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fromWorkspaces ...string) (string, error) {
out, err := g.runBasicCommand(ctx, "workspaces/create", map[string]any{
"provider": providerType,
"workspaceTool": g.globalOpts.WorkspaceTool,
"providerType": providerType,
"fromWorkspaceIDs": fromWorkspaces,
"workspaceTool": g.globalOpts.WorkspaceTool,
"directoryDataHome": g.globalOpts.WorkspaceDirectoryDataHome,
"env": g.globalOpts.Env,
})
if err != nil {
return "", err
Expand All @@ -19,156 +21,72 @@ func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string) (s
return strings.TrimSpace(out), nil
}

type DeleteWorkspaceOptions struct {
IgnoreNotFound bool
}

func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string, opts ...DeleteWorkspaceOptions) error {
var opt DeleteWorkspaceOptions
for _, o := range opts {
opt.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
}
func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string) error {
_, err := g.runBasicCommand(ctx, "workspaces/delete", map[string]any{
"id": workspaceID,
"ignoreNotFound": opt.IgnoreNotFound,
"workspaceTool": g.globalOpts.WorkspaceTool,
})

return err
}

type CreateDirectoryInWorkspaceOptions struct {
IgnoreExists bool
}

func (g *GPTScript) CreateDirectoryInWorkspace(ctx context.Context, workspaceID, dir string, opts ...CreateDirectoryInWorkspaceOptions) error {
var opt CreateDirectoryInWorkspaceOptions
for _, o := range opts {
opt.IgnoreExists = opt.IgnoreExists || o.IgnoreExists
}

_, err := g.runBasicCommand(ctx, "workspaces/mkdir", map[string]any{
"id": workspaceID,
"directoryName": dir,
"ignoreExists": opt.IgnoreExists,
"workspaceTool": g.globalOpts.WorkspaceTool,
})

return err
}

type DeleteDirectoryInWorkspaceOptions struct {
IgnoreNotFound bool
MustBeEmpty bool
}

func (g *GPTScript) DeleteDirectoryInWorkspace(ctx context.Context, workspaceID, dir string, opts ...DeleteDirectoryInWorkspaceOptions) error {
var opt DeleteDirectoryInWorkspaceOptions
for _, o := range opts {
o.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
o.MustBeEmpty = opt.MustBeEmpty || o.MustBeEmpty
}

_, err := g.runBasicCommand(ctx, "workspaces/rmdir", map[string]any{
"id": workspaceID,
"directoryName": dir,
"ignoreNotFound": opt.IgnoreNotFound,
"mustBeEmpty": opt.MustBeEmpty,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})

return err
}

type ListFilesInWorkspaceOptions struct {
SubDir string
NonRecursive bool
ExcludeHidden bool
}

type WorkspaceContent struct {
ID, Path, FileName string
Children []WorkspaceContent
Prefix string
}

func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string, opts ...ListFilesInWorkspaceOptions) (*WorkspaceContent, error) {
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string, opts ...ListFilesInWorkspaceOptions) ([]string, error) {
var opt ListFilesInWorkspaceOptions
for _, o := range opts {
if o.SubDir != "" {
opt.SubDir = o.SubDir
if o.Prefix != "" {
opt.Prefix = o.Prefix
}
opt.NonRecursive = opt.NonRecursive || o.NonRecursive
opt.ExcludeHidden = opt.ExcludeHidden || o.ExcludeHidden
}

out, err := g.runBasicCommand(ctx, "workspaces/list", map[string]any{
"id": workspaceID,
"subDir": opt.SubDir,
"excludeHidden": opt.ExcludeHidden,
"nonRecursive": opt.NonRecursive,
"prefix": opt.Prefix,
"workspaceTool": g.globalOpts.WorkspaceTool,
"json": true,
"env": g.globalOpts.Env,
})
if err != nil {
return nil, err
}

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

if len(content) == 0 {
return &WorkspaceContent{ID: workspaceID}, nil
}

return &content[0], nil
// The first line of the output is the workspace ID, ignore it.
return strings.Split(strings.TrimSpace(out), "\n")[1:], nil
}

type CreateFileInWorkspaceOptions struct {
MustNotExist bool
WithoutCreate bool
CreateDirs bool
}
func (g *GPTScript) RemoveAllWithPrefix(ctx context.Context, workspaceID, prefix string) error {
_, err := g.runBasicCommand(ctx, "workspaces/remove-all-with-prefix", map[string]any{
"id": workspaceID,
"prefix": prefix,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})

func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, filePath string, contents []byte, opts ...CreateFileInWorkspaceOptions) error {
var opt CreateFileInWorkspaceOptions
for _, o := range opts {
opt.MustNotExist = opt.MustNotExist || o.MustNotExist
opt.WithoutCreate = opt.WithoutCreate || o.WithoutCreate
opt.CreateDirs = opt.CreateDirs || o.CreateDirs
}
return err
}

func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, filePath string, contents []byte) error {
_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
"id": workspaceID,
"contents": base64.StdEncoding.EncodeToString(contents),
"filePath": filePath,
"mustNotExist": opt.MustNotExist,
"withoutCreate": opt.WithoutCreate,
"createDirs": opt.CreateDirs,
"workspaceTool": g.globalOpts.WorkspaceTool,
"base64EncodedInput": true,
"env": g.globalOpts.Env,
})

return err
}

type DeleteFileInWorkspaceOptions struct {
IgnoreNotFound bool
}

func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, filePath string, opts ...DeleteFileInWorkspaceOptions) error {
var opt DeleteFileInWorkspaceOptions
for _, o := range opts {
opt.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
}

func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, filePath string) error {
_, err := g.runBasicCommand(ctx, "workspaces/delete-file", map[string]any{
"id": workspaceID,
"filePath": filePath,
"ignoreNotFound": opt.IgnoreNotFound,
"workspaceTool": g.globalOpts.WorkspaceTool,
"id": workspaceID,
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})

return err
Expand All @@ -180,6 +98,7 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, workspaceID, filePa
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"base64EncodeOutput": true,
"env": g.globalOpts.Env,
})
if err != nil {
return nil, err
Expand Down
Loading