Skip to content

chore: bump tui to address sigint handling #495

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

Merged
merged 1 commit into from
Jun 13, 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379
github.com/gptscript-ai/tui v0.0.0-20240607210407-edb2d427695e
github.com/gptscript-ai/tui v0.0.0-20240613214935-03d62e2c7822
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
Expand Down Expand Up @@ -61,7 +61,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gptscript-ai/go-gptscript v0.0.0-20240604231423-7a845df843b1 // indirect
github.com/gptscript-ai/go-gptscript v0.0.0-20240613214812-8111c2b02d71 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hexops/autogold v1.3.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379 h1:vYnXoIyCXzaCEw0sYifQ4bDpsv3/fO/dZ2suEsTwCIo=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
github.com/gptscript-ai/go-gptscript v0.0.0-20240604231423-7a845df843b1 h1:SHoqsU8Ne2V4zfrFve9kQn4vcv4N4TItD6Oju+pzKV8=
github.com/gptscript-ai/go-gptscript v0.0.0-20240604231423-7a845df843b1/go.mod h1:h1yYzC0rgB5Kk7lwdba+Xs6cWkuJfLq6sPRna45OVG0=
github.com/gptscript-ai/tui v0.0.0-20240607210407-edb2d427695e h1:ETdyiFXN6i+Tb0I119XqGtMkPpDqig0m6MBrzGB7Mtc=
github.com/gptscript-ai/tui v0.0.0-20240607210407-edb2d427695e/go.mod h1:At6zmCk0XrJ2J1yo95fzbEDOGwaPPEMwxxcQGJx7IGE=
github.com/gptscript-ai/go-gptscript v0.0.0-20240613214812-8111c2b02d71 h1:WehkkausLuXI91ePpIVrzZ6eBmfFIU/HfNsSA1CHiwo=
github.com/gptscript-ai/go-gptscript v0.0.0-20240613214812-8111c2b02d71/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
github.com/gptscript-ai/tui v0.0.0-20240613214935-03d62e2c7822 h1:g1IR6P2F536lyDoFeGE1bLqnflXWipnUkZzIFoHIPjw=
github.com/gptscript-ai/tui v0.0.0-20240613214935-03d62e2c7822/go.mod h1:ZlyM+BRiD6mV04w+Xw2mXP1VKGEUbn8BvwrosWlplUo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
Expand Down
9 changes: 6 additions & 3 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ 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"`
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 @@ -439,8 +438,12 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {

if prg.IsChat() || r.ForceChat {
if !r.DisableTUI && !r.Debug && !r.DebugMessages {
return tui.Run(cmd.Context(), args[0], tui.RunOptions{
TrustedRepoPrefixes: []string{"github.com/gptscript-ai/context"},
// Don't use cmd.Context() because then sigint will cancel everything
return tui.Run(context.Background(), args[0], tui.RunOptions{
OpenAIAPIKey: r.OpenAIOptions.APIKey,
OpenAIBaseURL: r.OpenAIOptions.BaseURL,
DefaultModel: r.DefaultModel,
TrustedRepoPrefixes: []string{"github.com/gptscript-ai"},
DisableCache: r.DisableCache,
Input: strings.Join(args[1:], " "),
CacheDir: r.CacheDir,
Expand Down
14 changes: 13 additions & 1 deletion pkg/cli/sdk_server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cli

import (
"context"
"os"

"github.com/gptscript-ai/gptscript/pkg/sdkserver"
"github.com/spf13/cobra"
"golang.org/x/term"
)

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) {
Expand All @@ -21,7 +26,14 @@ func (c *SDKServer) Run(cmd *cobra.Command, _ []string) error {
return err
}

return sdkserver.Start(cmd.Context(), sdkserver.Options{
// Don't use cmd.Context() as we don't want to die on ctrl+c
ctx := context.Background()
if term.IsTerminal(int(os.Stdin.Fd())) {
// Only support CTRL+C if stdin is the terminal. When ran as a SDK it will be a pipe
ctx = cmd.Context()
}

return sdkserver.Start(ctx, sdkserver.Options{
Options: opts,
ListenAddress: c.ListenAddress,
Debug: c.Debug,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdkserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Options struct {
}

func Start(ctx context.Context, opts Options) error {
sigCtx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
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
Expand Down