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 standalone mode to SDKServer and server options #835

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions pkg/cli/sdk_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type SDKServer struct {
*GPTScript
Standalone bool `usage:"Standalone mode" local:"true"`
}

func (c *SDKServer) Customize(cmd *cobra.Command) {
Expand All @@ -37,5 +38,6 @@ func (c *SDKServer) Run(cmd *cobra.Command, _ []string) error {
Options: opts,
ListenAddress: c.ListenAddress,
Debug: c.Debug,
Standalone: c.Standalone,
})
}
18 changes: 11 additions & 7 deletions pkg/sdkserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Options struct {
ListenAddress string
Debug bool
DisableServerErrorLogging bool
Standalone bool
}

// Run will start the server and block until the server is shut down.
Expand All @@ -47,13 +48,15 @@ func Run(ctx context.Context, opts Options) error {

sigCtx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
defer cancel()
go func() {
// This is a hack. This server will be run as a forked process in the SDKs. The SDKs will hold stdin open for as long
// as it wants the server running. When stdin is closed (or the parent process dies), then this will unblock and the
// server will be shutdown.
_, _ = io.ReadAll(os.Stdin)
cancel()
}()
if !opts.Standalone {
go func() {
// This is a hack. This server will be run as a forked process in the SDKs. The SDKs will hold stdin open for as long
// as it wants the server running. When stdin is closed (or the parent process dies), then this will unblock and the
// server will be shutdown.
_, _ = io.ReadAll(os.Stdin)
cancel()
}()
}

return run(sigCtx, listener, opts)
}
Expand Down Expand Up @@ -159,6 +162,7 @@ func complete(opts ...Options) Options {
result.ListenAddress = types.FirstSet(opt.ListenAddress, result.ListenAddress)
result.Debug = types.FirstSet(opt.Debug, result.Debug)
result.DisableServerErrorLogging = types.FirstSet(opt.DisableServerErrorLogging, result.DisableServerErrorLogging)
result.Standalone = types.FirstSet(opt.Standalone, result.Standalone)
}

if result.ListenAddress == "" {
Expand Down