Skip to content
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
13 changes: 8 additions & 5 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ type agentOutput struct {

func (a *Session) Query(ctx context.Context, query string) (string, error) {
logger := logging.GetRequestLogger(ctx).With(zap.String("module", "agent"))
a.addToMessages(logger, ContentMessage{
Message: Message{Type: MessageTypeText},
Role: RoleTypeUser,
Content: query,
}, nil)
if query != "" {
a.addToMessages(logger, ContentMessage{
Message: Message{Type: MessageTypeText},
Role: RoleTypeUser,
Content: query,
}, nil)
}

var (
strBuilder strings.Builder
Expand Down Expand Up @@ -205,6 +207,7 @@ func (a *Session) startLoop(ctx context.Context) chan agentOutput {
Output: "error running tool",
ID: tool.ToolID,
}, out)
logger.Error("failed to execute tool", zap.String("tool", tool.Name), zap.Error(err))
continue
}
a.addToMessages(logger, ToolCallOutputMessage{
Expand Down
29 changes: 16 additions & 13 deletions pkg/engine/plan/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plan

import (
"context"
"errors"
"fmt"
"text/template"

Expand All @@ -17,7 +18,6 @@ import (

// TODO deprecate out
// TODO swap id for logger with id
// TODO pass logger in here

type Action struct {
configStr string
Expand All @@ -26,9 +26,12 @@ type Action struct {
exec actions.ActionExecutable
out string
id string
isGroup bool
}

var (
errExecutingAction = errors.New("executing action")
)

func (a *Action) ID() string {
return a.id
}
Expand Down Expand Up @@ -73,23 +76,23 @@ func (a *Action) Execute(ctx context.Context) (Step, error) {

resp, err := a.exec.Execute(ctx, cfg)
if err != nil {
logger.Debug("error executing action", zap.String("config", a.configStr), zap.Error(err))
span.RecordError(err)
if err2 := requestctx.AddRequestVariables(ctx, map[string]interface{}{requestctx.ErrorTagStripped: err.Error()}, ""); err2 != nil {
return nil, err2
}
if err2 := requestctx.AddRequestVariables(ctx, map[string]interface{}{a.out: fmt.Sprintf("error: %v", err)}, ""); err2 != nil {
return nil, err2
}
if a.fail != nil {
if err := requestctx.AddRequestVariables(ctx, map[string]interface{}{requestctx.ErrorTagStripped: err.Error()}, ""); err != nil {
return nil, err
}
logger.Debug("error executing action", zap.String("config", a.configStr), zap.Error(err))
return a.fail, nil
}
return nil, fmt.Errorf("error executing action: %s type: %s: %w", a.id, a.exec.Type(), err)
return nil, fmt.Errorf("%w: %w", errExecutingAction, err)
}
logger.Debug("action executed successfully", zap.Any("resp", resp))

if !a.isGroup {
if err = requestctx.AddRequestVariables(ctx, map[string]interface{}{a.out: resp}, ""); err != nil {
return nil, err
}
logger.Debug("action executed successfully", zap.Any("resp", resp))
if err = requestctx.AddRequestVariables(ctx, map[string]interface{}{a.out: resp}, ""); err != nil {
return nil, err
}

return a.next, nil
}
36 changes: 20 additions & 16 deletions pkg/engine/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,35 @@ func (p *Plan) executeStep(ctx context.Context, step Step, endValue string) (*ht
next, err = s.Execute(ctx)
}
if err != nil {
if errors.Is(err, errExecutingAction) {
return p.generateEndValue(ctx, logger, endValue)
}
return nil, fmt.Errorf("error executing step: %w", err)
}

if next != nil {
return p.executeStep(ctx, next, endValue)
}
return p.generateEndValue(ctx, logger, endValue)
}

if endValue != "" {
tmpl, err := requestctx.CreateTextTemplate(ctx, endValue, nil)
if err != nil {
return nil, err
}
val, err := requestctx.ExecuteTemplateFromContext(ctx, tmpl)
if err != nil {
return nil, err
}

logger.Debug("template generated", zap.String("template", val))

return &http.SfResponse{
Body: []byte(val),
}, nil
func (p *Plan) generateEndValue(ctx context.Context, logger *zap.Logger, endValue string) (*http.SfResponse, error) {
if endValue == "" {
return nil, nil
}
tmpl, err := requestctx.CreateTextTemplate(ctx, endValue, nil)
if err != nil {
return nil, err
}
val, err := requestctx.ExecuteTemplateFromContext(ctx, tmpl)
if err != nil {
return nil, err
}
logger.Debug("template generated", zap.String("template", val))

return nil, nil
return &http.SfResponse{
Body: []byte(val),
}, nil
}

func (p *Plan) Execute(ctx context.Context, id, endValue string) (*http.SfResponse, error) {
Expand Down
1 change: 0 additions & 1 deletion pkg/engine/plan/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (p *PlannerV2) generateActionStep(id string) (*Action, error) {
fail: failStep,
out: out,
exec: exec,
isGroup: a.Type == "group",
}, nil
}

Expand Down