Skip to content

fix: misc improvements related to creds and prompting #536

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 4 commits into from
Jun 25, 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
12 changes: 12 additions & 0 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -75,6 +76,17 @@ func SysPrompt(ctx context.Context, envs []string, input string, _ chan<- string
func sysPrompt(ctx context.Context, req types.Prompt) (_ string, err error) {
defer context2.GetPauseFuncFromCtx(ctx)()()

if req.Message != "" && len(req.Fields) == 1 && strings.TrimSpace(req.Fields[0]) == "" {
var errs []error
_, err := fmt.Fprintln(os.Stderr, req.Message)
errs = append(errs, err)
_, err = fmt.Fprintln(os.Stderr, "Press enter to continue...")
errs = append(errs, err)
_, err = fmt.Fscanln(os.Stdin)
errs = append(errs, err)
return "", errors.Join(errs...)
}

if req.Message != "" && len(req.Fields) != 1 {
_, _ = fmt.Fprintln(os.Stderr, req.Message)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
Time: time.Now(),
CallContext: callCtx.GetCallContext(),
Type: EventTypeCallFinish,
Content: getFinishEventContent(*state, callCtx),
Content: getEventContent(*state.Continuation.Result, callCtx),
})
if callCtx.Tool.Chat {
return &State{
Expand Down Expand Up @@ -681,7 +681,7 @@ func streamProgress(callCtx *engine.Context, monitor Monitor) (chan<- types.Comp
CallContext: callCtx.GetCallContext(),
Type: EventTypeCallProgress,
ChatCompletionID: status.CompletionID,
Content: message.String(),
Content: getEventContent(message.String(), *callCtx),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to prevent credential tool output from leaking to the terminal.

})
} else {
monitor.Event(Event{
Expand Down Expand Up @@ -821,13 +821,13 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
return state, callResults, nil
}

func getFinishEventContent(state State, callCtx engine.Context) string {
// If it is a credential tool, the finish event contains its output, which is sensitive, so we don't return it.
func getEventContent(content string, callCtx engine.Context) string {
// If it is a credential tool, the progress and finish events may contain its output, which is sensitive, so we don't return it.
if callCtx.ToolCategory == engine.CredentialToolCategory {
return ""
}

return *state.Continuation.Result
return content
}

func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env []string) ([]string, error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/types/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ func TestParseCredentialArgs(t *testing.T) {
wantErr: true,
},
{
name: "invalid input",
toolName: "myCredentialTool",
input: `{"asdf":"asdf"`,
wantErr: true,
name: "invalid input",
toolName: "myCredentialTool",
input: `{"asdf":"asdf"`,
expectedName: "myCredentialTool",
expectedAlias: "",
wantErr: false,
},
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/types/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ func ParseCredentialArgs(toolName string, input string) (string, string, map[str

inputMap := make(map[string]any)
if input != "" {
err := json.Unmarshal([]byte(input), &inputMap)
if err != nil {
return "", "", nil, fmt.Errorf("failed to unmarshal input: %w", err)
}
// Sometimes this function can be called with input that is not a JSON string.
// This typically happens during chat mode.
// That's why we ignore the error if this fails to unmarshal.
_ = json.Unmarshal([]byte(input), &inputMap)
}

fields, err := shlex.Split(toolName)
Expand Down
Loading