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

chore: allow easy embedding on gptscript #497

Merged
merged 2 commits into from
Jun 14, 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
14 changes: 14 additions & 0 deletions internal/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package internal

import (
"io/fs"
"os"
)

var FS fs.FS = defaultFS{}

type defaultFS struct{}

func (d defaultFS) Open(name string) (fs.File, error) {
return os.Open(name)
}
19 changes: 1 addition & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
package main

import (
"os"

"github.com/acorn-io/cmd"
"github.com/gptscript-ai/gptscript/pkg/cli"
"github.com/gptscript-ai/gptscript/pkg/daemon"
"github.com/gptscript-ai/gptscript/pkg/mvl"

// Load all VCS
_ "github.com/gptscript-ai/gptscript/pkg/loader/vcs"
)

var log = mvl.Package()

func main() {
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
if os.Getenv("GPTSCRIPT_DEBUG") == "true" {
mvl.SetDebug()
}
if err := daemon.SysDaemon(); err != nil {
log.Debugf("failed running daemon: %v", err)
}
os.Exit(0)
}
cmd.Main(cli.New())
cli.Main()
}
3 changes: 2 additions & 1 deletion pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type GPTScript struct {
Assemble bool `usage:"Assemble tool to a single artifact, saved to --output" hidden:"true" local:"true"`
ListModels bool `usage:"List the models available and exit" local:"true"`
ListTools bool `usage:"List built-in tools and exit" local:"true"`
ListenAddress string `usage:"Server listen address" default:"127.0.0.1:0" hidden:"true"`
Chdir string `usage:"Change current working directory" short:"C"`
Daemon bool `usage:"Run tool as a daemon" local:"true" hidden:"true"`
Ports string `usage:"The port range to use for ephemeral daemon ports (ex: 11000-12000)" hidden:"true"`
Expand Down Expand Up @@ -218,7 +219,7 @@ func (r *GPTScript) PersistentPre(*cobra.Command, []string) error {
}
}

_ = os.Setenv(system.BinEnvVar, system.Bin())
system.SetBinToSelf()

if r.DefaultModel != "" {
builtin.SetDefaultModel(r.DefaultModel)
Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli

import (
"os"

"github.com/acorn-io/cmd"
"github.com/gptscript-ai/gptscript/pkg/daemon"
"github.com/gptscript-ai/gptscript/pkg/mvl"
)

func Main() {
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
if os.Getenv("GPTSCRIPT_DEBUG") == "true" {
mvl.SetDebug()
}
if err := daemon.SysDaemon(); err != nil {
log.Debugf("failed running daemon: %v", err)
}
os.Exit(0)
}
cmd.Main(New())
}
2 changes: 1 addition & 1 deletion pkg/cli/sdk_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

type SDKServer struct {
*GPTScript
ListenAddress string `usage:"Server listen address" default:"127.0.0.1:0" local:"true"`
}

func (c *SDKServer) Customize(cmd *cobra.Command) {
cmd.Use = "sys.sdkserver"
cmd.Args = cobra.NoArgs
cmd.Aliases = []string{"sdkserver"}
cmd.Hidden = true
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/embedded/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package embedded

import (
"io/fs"
"os"
"strings"

"github.com/gptscript-ai/gptscript/internal"
"github.com/gptscript-ai/gptscript/pkg/cli"
"github.com/gptscript-ai/gptscript/pkg/system"
)

type Options struct {
FS fs.FS
}

func Run(opts ...Options) bool {
for _, opt := range opts {
if opt.FS == nil {
internal.FS = opt.FS
}
}

system.SetBinToSelf()
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "sys.") {
cli.Main()
return true
}

return false
}
8 changes: 4 additions & 4 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strconv"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi2conv"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gptscript-ai/gptscript/internal"
"github.com/gptscript-ai/gptscript/pkg/assemble"
"github.com/gptscript-ai/gptscript/pkg/builtin"
"github.com/gptscript-ai/gptscript/pkg/cache"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *source) String() string {
}

func openFile(path string) (io.ReadCloser, bool, error) {
f, err := os.Open(path)
f, err := internal.FS.Open(path)
if errors.Is(err, fs.ErrNotExist) {
return nil, false, nil
} else if err != nil {
Expand All @@ -74,10 +74,10 @@ func loadLocal(base *source, name string) (*source, bool, error) {
// We want to keep all strings in / format, and only convert to platform specific when reading
filePath := path.Join(base.Path, name)

if s, err := os.Stat(filepath.Clean(filePath)); err == nil && s.IsDir() {
if s, err := fs.Stat(internal.FS, filepath.Clean(filePath)); err == nil && s.IsDir() {
for _, def := range types.DefaultFiles {
toolPath := path.Join(filePath, def)
if s, err := os.Stat(filepath.Clean(toolPath)); err == nil && !s.IsDir() {
if s, err := fs.Stat(internal.FS, filepath.Clean(toolPath)); err == nil && !s.IsDir() {
filePath = toolPath
break
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func normalize(key string) string {
}

func toBool(line string) (bool, error) {
if line == "true" {
line = normalize(line)
if line == "true" || line == "t" {
return true, nil
} else if line != "false" {
return false, fmt.Errorf("invalid boolean parameter, must be \"true\" or \"false\", got [%s]", line)
Expand Down
6 changes: 6 additions & 0 deletions pkg/system/bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import "os"

const BinEnvVar = "GPTSCRIPT_BIN"

func SetBinToSelf() {
if err := os.Setenv(BinEnvVar, Bin()); err != nil {
panic(err)
}
}

func Bin() string {
bin := os.Getenv(BinEnvVar)
if bin != "" {
Expand Down
2 changes: 0 additions & 2 deletions pkg/system/currentbin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !linux

package system

import (
Expand Down
8 changes: 0 additions & 8 deletions pkg/system/currentbin_linux.go

This file was deleted.