Skip to content
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
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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 err := daemon.SysDaemon(); err != nil {
log.Fatalf("failed running daemon: %v", err)
}
os.Exit(0)
}
cmd.Main(cli.New())
}
24 changes: 24 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package daemon

import (
"context"
"io"
"os"
"os/exec"
)

func SysDaemon() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
_, _ = io.ReadAll(os.Stdin)
cancel()
}()

cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
4 changes: 4 additions & 0 deletions pkg/engine/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
return "", err
}

// Loop back to gptscript to help with process supervision
cmd.Args = append([]string{os.Args[0], "sys.daemon", cmd.Path}, cmd.Args[1:]...)
cmd.Path = self()

cmd.Stdin = r
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down
27 changes: 27 additions & 0 deletions pkg/engine/self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !linux

package engine

import (
"os"
"os/exec"
"path/filepath"
)

// Copied from github.com/moby/moby/pkg/reexec d25b0bd7ea6ce17ca085c54d5965eeeb66417e52

func self() string {
name := os.Args[0]
if filepath.Base(name) == name {
if lp, err := exec.LookPath(name); err == nil {
return lp
}
}
// handle conversion of relative paths to absolute
if absName, err := filepath.Abs(name); err == nil {
return absName
}
// if we couldn't get absolute name, return original
// (NOTE: Go only errors on Abs() if os.Getwd fails)
return name
}
5 changes: 5 additions & 0 deletions pkg/engine/self_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package engine

func self() string {
return "/proc/self/exe"
}