Skip to content

fix: make prompt explicit #22

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
May 31, 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
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ None of the options is required, and the defaults will reduce the number of call
- `inlcudeEvents`: Whether to include the streaming of events. Default (false). Note that if this is true, you must stream the events. See below for details.
- `chatState`: The chat state to continue, or null to start a new chat and return the state
- `confirm`: Prompt before running potentially dangerous commands
- `prompt`: Allow prompting of the user

## Functions

Expand Down Expand Up @@ -298,7 +299,7 @@ func runFileWithConfirm(ctx context.Context) (string, error) {
}

for event := range run.Events() {
if event.Type == gptscript.EventTypeCallConfirm {
if event.Call != nil && event.Call.Type == gptscript.EventTypeCallConfirm {
// event.Tool has the information on the command being run.
// and event.Input will have the input to the command being run.

Expand All @@ -319,6 +320,60 @@ func runFileWithConfirm(ctx context.Context) (string, error) {
}
```

### Prompt

Using the `Prompt: true` option allows a script to prompt a user for input. In order to do this, a caller should look for the `Prompt` event. This also means that `IncludeEvent` should be `true`. Note that if a `Prompt` event occurs when it has not explicitly been allowed, then the run will error.

```go
package main

import (
"context"

"github.com/gptscript-ai/go-gptscript"
)

func runFileWithPrompt(ctx context.Context) (string, error) {
opts := gptscript.Options{
DisableCache: &[]bool{true}[0],
Input: "--input hello",
Prompt: true,
IncludeEvents: true,
}

client, err := gptscript.NewClient()
if err != nil {
return "", err
}
defer client.Close()

run, err := client.Run(ctx, "./hello.gpt", opts)
if err != nil {
return "", err
}

for event := range run.Events() {
if event.Prompt != nil {
// event.Prompt has the information to prompt the user.

err = client.PromptResponse(ctx, gptscript.PromptResponse{
ID: event.Prompt.ID,
// Responses is a map[string]string of Fields to values
Responses: map[string]string{
event.Prompt.Fields[0]: "Some Value",
},
})
if err != nil {
// Handle error
}
}

// Process event...
}

return run.Text()
}
```

## Types

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (c *client) Confirm(ctx context.Context, resp AuthResponse) error {
}

func (c *client) PromptResponse(ctx context.Context, resp PromptResponse) error {
_, err := c.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Response)
_, err := c.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
return err
}

Expand Down
44 changes: 41 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func TestPrompt(t *testing.T) {
},
}

run, err := c.Evaluate(context.Background(), Options{IncludeEvents: true}, tools...)
run, err := c.Evaluate(context.Background(), Options{IncludeEvents: true, Prompt: true}, tools...)
if err != nil {
t.Errorf("Error executing tool: %v", err)
}
Expand Down Expand Up @@ -859,8 +859,8 @@ func TestPrompt(t *testing.T) {
}

if err = c.PromptResponse(context.Background(), PromptResponse{
ID: promptFrame.ID,
Response: map[string]string{promptFrame.Fields[0]: "Clicky"},
ID: promptFrame.ID,
Responses: map[string]string{promptFrame.Fields[0]: "Clicky"},
}); err != nil {
t.Errorf("Error responding: %v", err)
}
Expand Down Expand Up @@ -892,6 +892,44 @@ func TestPrompt(t *testing.T) {
}
}

func TestPromptWithoutPromptAllowed(t *testing.T) {
tools := []fmt.Stringer{
&ToolDef{
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
Tools: []string{"sys.prompt"},
},
}

run, err := c.Evaluate(context.Background(), Options{IncludeEvents: true}, tools...)
if err != nil {
t.Errorf("Error executing tool: %v", err)
}

// Wait for the prompt event
var promptFrame *PromptFrame
for e := range run.Events() {
if e.Prompt != nil {
if e.Prompt.Type == EventTypePrompt {
promptFrame = e.Prompt
break
}
}
}

if promptFrame != nil {
t.Errorf("Prompt call event shouldn't happen")
}

_, err = run.Text()
if err == nil || !strings.Contains(err.Error(), "prompt event occurred") {
t.Errorf("Error reading output: %v", err)
}

if run.State() != Error {
t.Errorf("Unexpected state: %v", run.State())
}
}

func TestGetCommand(t *testing.T) {
currentEnvVar := os.Getenv("GPTSCRIPT_BIN")
t.Cleanup(func() {
Expand Down
11 changes: 10 additions & 1 deletion frame.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gptscript

import "time"
import (
"fmt"
"time"
)

type ToolCategory string

Expand Down Expand Up @@ -103,3 +106,9 @@ type PromptFrame struct {
Fields []string `json:"fields,omitempty"`
Sensitive bool `json:"sensitive,omitempty"`
}

func (p *PromptFrame) String() string {
return fmt.Sprintf(`Message: %s
Fields: %v
Sensitive: %v`, p.Message, p.Fields, p.Sensitive)
}
1 change: 1 addition & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Options struct {
Workspace string `json:"workspace"`
ChatState string `json:"chatState"`
IncludeEvents bool `json:"includeEvents"`
Prompt bool `json:"prompt"`
}
4 changes: 2 additions & 2 deletions prompt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gptscript

type PromptResponse struct {
ID string `json:"id,omitempty"`
Response map[string]string `json:"response,omitempty"`
ID string `json:"id,omitempty"`
Responses map[string]string `json:"response,omitempty"`
}
21 changes: 15 additions & 6 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,21 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
r.err = fmt.Errorf("failed to process stderr, invalid type: %T", out)
}
} else {
if r.opts.IncludeEvents {
var event Frame
if err := json.Unmarshal(line, &event); err != nil {
slog.Debug("failed to unmarshal event", "error", err, "event", string(line))
}
var event Frame
if err := json.Unmarshal(line, &event); err != nil {
slog.Debug("failed to unmarshal event", "error", err, "event", string(line))
}

if event.Prompt != nil && !r.opts.Prompt {
r.state = Error
r.err = fmt.Errorf("prompt event occurred when prompt was not allowed: %s", event.Prompt)
// Ignore the error because it is the same as the above error.
_ = r.Close()

return
}

if r.opts.IncludeEvents {
r.events <- event
}
}
Expand All @@ -304,7 +313,7 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
if err := context.Cause(cancelCtx); !errors.Is(err, context.Canceled) && r.err == nil {
r.state = Error
r.err = err
} else if r.state != Continue {
} else if r.state != Continue && r.state != Error {
r.state = Finished
}
}
Expand Down