Skip to content

Commit 096606d

Browse files
chore: allow easy embedding on gptscript
1 parent 0e05bfb commit 096606d

File tree

8 files changed

+81
-24
lines changed

8 files changed

+81
-24
lines changed

internal/fs.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package internal
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
)
7+
8+
var FS fs.FS = defaultFS{}
9+
10+
type defaultFS struct{}
11+
12+
func (d defaultFS) Open(name string) (fs.File, error) {
13+
return os.Open(name)
14+
}

main.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
package main
22

33
import (
4-
"os"
5-
6-
"github.com/acorn-io/cmd"
74
"github.com/gptscript-ai/gptscript/pkg/cli"
8-
"github.com/gptscript-ai/gptscript/pkg/daemon"
9-
"github.com/gptscript-ai/gptscript/pkg/mvl"
10-
115
// Load all VCS
126
_ "github.com/gptscript-ai/gptscript/pkg/loader/vcs"
137
)
148

15-
var log = mvl.Package()
16-
179
func main() {
18-
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
19-
if os.Getenv("GPTSCRIPT_DEBUG") == "true" {
20-
mvl.SetDebug()
21-
}
22-
if err := daemon.SysDaemon(); err != nil {
23-
log.Debugf("failed running daemon: %v", err)
24-
}
25-
os.Exit(0)
26-
}
27-
cmd.Main(cli.New())
10+
cli.Main()
2811
}

pkg/cli/gptscript.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (r *GPTScript) PersistentPre(*cobra.Command, []string) error {
218218
}
219219
}
220220

221-
_ = os.Setenv(system.BinEnvVar, system.Bin())
221+
system.SetBinToSelf()
222222

223223
if r.DefaultModel != "" {
224224
builtin.SetDefaultModel(r.DefaultModel)

pkg/cli/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
6+
"github.com/acorn-io/cmd"
7+
"github.com/gptscript-ai/gptscript/pkg/daemon"
8+
"github.com/gptscript-ai/gptscript/pkg/mvl"
9+
)
10+
11+
func Main() {
12+
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
13+
if os.Getenv("GPTSCRIPT_DEBUG") == "true" {
14+
mvl.SetDebug()
15+
}
16+
if err := daemon.SysDaemon(); err != nil {
17+
log.Debugf("failed running daemon: %v", err)
18+
}
19+
os.Exit(0)
20+
}
21+
cmd.Main(New())
22+
}

pkg/embedded/embed.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package embedded
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"strings"
7+
8+
"github.com/gptscript-ai/gptscript/internal"
9+
"github.com/gptscript-ai/gptscript/pkg/cli"
10+
"github.com/gptscript-ai/gptscript/pkg/system"
11+
)
12+
13+
type Options struct {
14+
FS fs.FS
15+
}
16+
17+
func Run(opts ...Options) bool {
18+
for _, opt := range opts {
19+
if opt.FS == nil {
20+
internal.FS = opt.FS
21+
}
22+
}
23+
24+
system.SetBinToSelf()
25+
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "sys.") {
26+
cli.Main()
27+
return true
28+
}
29+
30+
return false
31+
}

pkg/loader/loader.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"io/fs"
11-
"os"
1211
"path"
1312
"path/filepath"
1413
"strconv"
@@ -19,6 +18,7 @@ import (
1918
"github.com/getkin/kin-openapi/openapi2"
2019
"github.com/getkin/kin-openapi/openapi2conv"
2120
"github.com/getkin/kin-openapi/openapi3"
21+
"github.com/gptscript-ai/gptscript/internal"
2222
"github.com/gptscript-ai/gptscript/pkg/assemble"
2323
"github.com/gptscript-ai/gptscript/pkg/builtin"
2424
"github.com/gptscript-ai/gptscript/pkg/cache"
@@ -61,7 +61,7 @@ func (s *source) String() string {
6161
}
6262

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

77-
if s, err := os.Stat(filepath.Clean(filePath)); err == nil && s.IsDir() {
77+
if s, err := fs.Stat(internal.FS, filepath.Clean(filePath)); err == nil && s.IsDir() {
7878
for _, def := range types.DefaultFiles {
7979
toolPath := path.Join(filePath, def)
80-
if s, err := os.Stat(filepath.Clean(toolPath)); err == nil && !s.IsDir() {
80+
if s, err := fs.Stat(internal.FS, filepath.Clean(toolPath)); err == nil && !s.IsDir() {
8181
filePath = toolPath
8282
break
8383
}

pkg/parser/parser.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func normalize(key string) string {
2424
}
2525

2626
func toBool(line string) (bool, error) {
27-
if line == "true" {
27+
line = normalize(line)
28+
if line == "true" || line == "t" {
2829
return true, nil
2930
} else if line != "false" {
3031
return false, fmt.Errorf("invalid boolean parameter, must be \"true\" or \"false\", got [%s]", line)

pkg/system/bin.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import "os"
44

55
const BinEnvVar = "GPTSCRIPT_BIN"
66

7+
func SetBinToSelf() {
8+
if err := os.Setenv(BinEnvVar, Bin()); err != nil {
9+
panic(err)
10+
}
11+
}
12+
713
func Bin() string {
814
bin := os.Getenv(BinEnvVar)
915
if bin != "" {

0 commit comments

Comments
 (0)