Skip to content

enhance: add context to confirm call to get run context information #219

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
Apr 4, 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
6 changes: 3 additions & 3 deletions pkg/confirm/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Confirm interface {
Confirm(prompt string) error
Confirm(ctx context.Context, prompt string) error
}

type confirmer struct{}
Expand All @@ -23,13 +23,13 @@ func Promptf(ctx context.Context, fmtString string, args ...any) error {
if !ok {
return nil
}
return c.Confirm(fmt.Sprintf(fmtString, args...))
return c.Confirm(ctx, fmt.Sprintf(fmtString, args...))
}

type TextPrompt struct {
}

func (t TextPrompt) Confirm(prompt string) error {
func (t TextPrompt) Confirm(_ context.Context, prompt string) error {
var result bool
err := survey.AskOne(&survey.Confirm{
Message: prompt,
Expand Down
13 changes: 12 additions & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ func (c *Context) SubCall(ctx context.Context, toolID, callID string) (Context,
}, nil
}

type engineContext struct{}

func FromContext(ctx context.Context) (*Context, bool) {
c, ok := ctx.Value(engineContext{}).(*Context)
return c, ok
}

func (c *Context) WrappedContext() context.Context {
return context.WithValue(c.Ctx, engineContext{}, c)
}

func (e *Engine) Start(ctx Context, input string) (*Return, error) {
tool := ctx.Tool

Expand All @@ -120,7 +131,7 @@ func (e *Engine) Start(ctx Context, input string) (*Return, error) {
} else if tool.IsOpenAPI() {
return e.runOpenAPI(tool, input)
}
s, err := e.runCommand(ctx.Ctx, tool, input)
s, err := e.runCommand(ctx.WrappedContext(), tool, input)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, inp
}
}

func streamProgress(callCtx *engine.Context, monitor Monitor) (chan types.CompletionStatus, func()) {
func streamProgress(callCtx *engine.Context, monitor Monitor) (chan<- types.CompletionStatus, func()) {
progress := make(chan types.CompletionStatus)

wg := sync.WaitGroup{}
Expand Down