Standardize action results

This commit is contained in:
mudler
2025-03-01 17:27:07 +01:00
parent 8492c95cb6
commit 5b4f618ca3
22 changed files with 108 additions and 67 deletions

View File

@@ -0,0 +1,13 @@
package action_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestAction(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Agent Action test suite")
}

View File

@@ -75,15 +75,16 @@ func (a *CustomAction) initializeInterpreter() error {
return nil
}
func (a *CustomAction) Run(ctx context.Context, params ActionParams) (string, error) {
func (a *CustomAction) Run(ctx context.Context, params ActionParams) (ActionResult, error) {
v, err := a.i.Eval(fmt.Sprintf("%s.Run", a.config["name"]))
if err != nil {
return "", err
return ActionResult{}, err
}
run := v.Interface().(func(map[string]interface{}) (string, error))
run := v.Interface().(func(map[string]interface{}) (string, map[string]interface{}, error))
return run(params)
res, meta, err := run(params)
return ActionResult{Result: res, Metadata: meta}, err
}
func (a *CustomAction) Definition() ActionDefinition {

View File

@@ -23,18 +23,18 @@ type Params struct {
Foo string
}
func Run(config map[string]interface{}) (string, error) {
func Run(config map[string]interface{}) (string, map[string]interface{}, error) {
p := Params{}
b, err := json.Marshal(config)
if err != nil {
return "", err
return "",map[string]interface{}{}, err
}
if err := json.Unmarshal(b, &p); err != nil {
return "", err
return "",map[string]interface{}{}, err
}
return p.Foo, nil
return p.Foo,map[string]interface{}{}, nil
}
func Definition() map[string][]string {
@@ -79,7 +79,7 @@ return []string{"foo"}
"Foo": "bar",
})
Expect(err).ToNot(HaveOccurred())
Expect(runResult).To(Equal("bar"))
Expect(runResult.Result).To(Equal("bar"))
})
})

View File

@@ -28,6 +28,11 @@ func NewContext(ctx context.Context, cancel context.CancelFunc) *ActionContext {
type ActionParams map[string]interface{}
type ActionResult struct {
Result string
Metadata map[string]interface{}
}
func (ap ActionParams) Read(s string) error {
err := json.Unmarshal([]byte(s), &ap)
return err

View File

@@ -21,8 +21,8 @@ type IntentResponse struct {
Reasoning string `json:"reasoning"`
}
func (a *IntentAction) Run(context.Context, ActionParams) (string, error) {
return "no-op", nil
func (a *IntentAction) Run(context.Context, ActionParams) (ActionResult, error) {
return ActionResult{}, nil
}
func (a *IntentAction) Definition() ActionDefinition {

View File

@@ -18,8 +18,8 @@ type ConversationActionResponse struct {
Message string `json:"message"`
}
func (a *ConversationAction) Run(context.Context, ActionParams) (string, error) {
return "no-op", nil
func (a *ConversationAction) Run(context.Context, ActionParams) (ActionResult, error) {
return ActionResult{}, nil
}
func (a *ConversationAction) Definition() ActionDefinition {

View File

@@ -12,8 +12,8 @@ func NewStop() *StopAction {
type StopAction struct{}
func (a *StopAction) Run(context.Context, ActionParams) (string, error) {
return "no-op", nil
func (a *StopAction) Run(context.Context, ActionParams) (ActionResult, error) {
return ActionResult{}, nil
}
func (a *StopAction) Definition() ActionDefinition {

View File

@@ -19,8 +19,8 @@ type ReasoningResponse struct {
Reasoning string `json:"reasoning"`
}
func (a *ReasoningAction) Run(context.Context, ActionParams) (string, error) {
return "no-op", nil
func (a *ReasoningAction) Run(context.Context, ActionParams) (ActionResult, error) {
return ActionResult{}, nil
}
func (a *ReasoningAction) Definition() ActionDefinition {

View File

@@ -33,8 +33,8 @@ type StateResult struct {
Goal string `json:"goal"`
}
func (a *StateAction) Run(context.Context, ActionParams) (string, error) {
return "internal state has been updated", nil
func (a *StateAction) Run(context.Context, ActionParams) (ActionResult, error) {
return ActionResult{Result: "internal state has been updated"}, nil
}
func (a *StateAction) Definition() ActionDefinition {

View File

@@ -23,7 +23,7 @@ type ActionCurrentState struct {
// Actions is something the agent can do
type Action interface {
Run(ctx context.Context, action action.ActionParams) (string, error)
Run(ctx context.Context, action action.ActionParams) (action.ActionResult, error)
Definition() action.ActionDefinition
}

View File

@@ -242,9 +242,12 @@ func (a *Agent) Memory() RAGDB {
func (a *Agent) runAction(chosenAction Action, params action.ActionParams) (result string, err error) {
for _, action := range a.systemInternalActions() {
if action.Definition().Name == chosenAction.Definition().Name {
if result, err = action.Run(a.actionContext, params); err != nil {
res, err := action.Run(a.actionContext, params)
if err != nil {
return "", fmt.Errorf("error running action: %w", err)
}
result = res.Result
}
}

View File

@@ -36,7 +36,7 @@ type TestAction struct {
responseN int
}
func (a *TestAction) Run(context.Context, action.ActionParams) (string, error) {
func (a *TestAction) Run(context.Context, action.ActionParams) (action.ActionResult, error) {
res := a.response[a.responseN]
a.responseN++
@@ -44,7 +44,7 @@ func (a *TestAction) Run(context.Context, action.ActionParams) (string, error) {
a.responseN = 0
}
return res, nil
return action.ActionResult{Result: res}, nil
}
func (a *TestAction) Definition() action.ActionDefinition {