Skip to content

Commit

Permalink
feat: add standalone mode to SDKServer and server options
Browse files Browse the repository at this point in the history
Added a new `Standalone` boolean field to the `SDKServer` and `Options` structs to support running the server in standalone mode. Updated the server run logic to conditionally skip the stdin read hack when in standalone mode.
  • Loading branch information
rinor committed Sep 18, 2024
1 parent 780e07e commit d4aced9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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

0 comments on commit d4aced9

Please sign in to comment.